Month: November 2016

Email sending using file template

HI, below sample code used to send email using Email tempalte formate in multiple lines and header formate.

static void sendMail(Email _defEmail, SysEmailId _emailTemplate, PurchTable PurchTable)
{
SysEmailParameters parameters = SysEmailParameters::find();
SMTPRelayServerName relayServer;
SMTPPortNumber portNumber;
SMTPUserName userName;
SMTPPassword password;
Str1260 subject;
Notes msgbody ;
InteropPermission interopPermission;
SysMailer mailer;
System.Exception e;

SysEmailTable table;
SysEmailMessageTable message;

str newMessageBody;

;

if(_defEmail)
{
if (parameters.SMTPRelayServerName)
relayServer = parameters.SMTPRelayServerName;
else
relayServer = parameters.SMTPServerIPAddress;

portNumber = parameters.SMTPPortNumber;
userName = parameters.SMTPUserName;
password = SysEmailParameters::password();

table = SysEmailTable::find(_emailTemplate);
message = SysEmailMessageTable::find(table.EmailId, table.DefaultLanguage);
if (message)
{
newMessageBody = VendorSendEmail::renderLetter(message,PurchTable);
}
subject = message.Subject;
msgbody = newMessageBody;

CodeAccessPermission::revertAssert();

try
{
interopPermission = new InteropPermission(InteropKind::ComInterop);
interopPermission.assert();
mailer = new SysMailer();
mailer.SMTPRelayServer(relayServer,portNumber,userName,password, parameters.NTLM);
mailer.fromAddress(SysUserInfo::find().Email);
mailer.tos().appendAddress(_defEmail);
mailer.subject(subject);
mailer.htmlBody(msgbody);
mailer.sendMail();
CodeAccessPermission::revertAssert();
info(“@CIT957”);
}
catch (Exception::CLRError)
{
e = ClrInterop::getLastException();

while (e)
{
info(e.get_Message());
e = e.get_InnerException();
}
CodeAccessPermission::revertAssert();
info (“error”);
}
}
else
error(“done”);

}

Advertisements

Reading text file data from folder

HI, Below sample code used to read multiple files

  1. Read data from file,Write to new file in different folder
  2. Delete files

Read and write file:

public void writeToHistoryFile(FilePath _fromFilePath)//Source filepath
{
#File
System.IO.StreamWriter streamWriterHistory;
Counter countFiles;
FileName fileNameHistory;
str inputLineHistory;
Set permissionSetHistory;
str directoryHistory, filePathHistory;
container fileListConHistory, record;
CommaTextIo commaTextIoHistory;

directoryHistory = _fromFilePath;
if(directoryHistory == “”)
{
return;
}
fileListConHistory = this.findMatchingFiles(directoryHistory);
//permissionSetHistory.add(new InteropPermission(InteropKind::ClrInterop));
for (countFiles=1; countFiles <= conlen(fileListConHistory); countFiles++)
{
ttsBegin;
inputLineHistory = “”;
fileNameHistory = “Newfolderpath”;

fileNameHistory += “\\newfilename”+”_”+Winapi::createGUID()+”_”;
fileNameHistory += date2Str(today(), 213, 2, DateSeparator::None, 2, DateSeparator::None, 4);
fileNameHistory +=” “;
fileNameHistory += time2str(timeNow(),TimeSeparator::Dot, TimeFormat::AMPM);
fileNameHistory += “.txt”;
streamWriterHistory= new System.IO.StreamWriter(fileNameHistory,
false,
System.Text.Encoding::GetEncoding(28591));

permissionSetHistory = new Set(Types::Class);

permissionSetHistory.add(new InteropPermission(InteropKind::ClrInterop));
filePathHistory = conPeek(fileListConHistory,countFiles);

permissionSetHistory.add(new FileIOPermission(filePathHistory, ‘r’));
permissionSetHistory.add(new FileIOPermission(filePathHistory, ‘rw’));
CodeAccessPermission::assertMultiple(permissionSetHistory);

commaTextIoHistory = new CommaTextIo(filePathHistory,#io_read);
if (! commaTextIoHistory || commaTextIoHistory.status() != IO_Status::Ok)
{
throw error(“@SYS19358”);
}
while (commaTextIoHistory.status() == IO_Status::Ok)
{
record = commaTextIoHistory.read();

if(!record)
{
CodeAccessPermission::revertAssert();
break;
}

//record = conIns(record, conLen(record) +1);
if(!inputLineHistory)
inputLineHistory = con2Str(record);
else
inputLineHistory = “\n” + con2Str(record);

streamWriterHistory.WriteLine(inputLineHistory,true);
}
CodeAccessPermission::revertAssert();

// }
streamWriterHistory.Close();
commaTextIoHistory.finalize();
ttsCommit;
}
}

Delete file:

static boolean importFromWorkstation(FilePath _deleteFile = “”)
{
//MPSWorkstation _tWorkstation
int iNumberOfFiles;
FileName sFileName,fileNamePerm, inFile;
InteropPermission interopPerm;
Set permissionSet, permSetFiles;
System.Array files;
int i, j;
container fList;
#File
;

permissionSet = new Set(Types::Class);
permissionSet.add(new InterOpPermission(InteropKind::ClrInterop));

CodeAccessPermission::assertMultiple(permissionSet);

files = System.IO.Directory::GetFiles(_deleteFile);
for( i=0; i<ClrInterop::getAnyTypeForObject(files.get_Length()); i++ )
{
fList = conins(fList, conlen(fList)+1, ClrInterop::getAnyTypeForObject(files.GetValue(i)));
}

for(j=1;j<=conlen(fList); j++)

{
sFileName = conpeek(fList, j);
inFile = sFileName;
ttsbegin;

System.IO.File::Delete(inFile);

ttscommit;
}
CodeAccessPermission::revertAssert();

return true;
}

getting standard Shipping and Tax for SO

HI, Below sample code used to get shipping charges for sales order.

static real shippingCharges(SalesId _salesId)
{
QueryRun queryRun;
OSSDeliveryChargeTable deliveryChargeTable, table;
DlvMode mode;
SalesTable _salesTable = SalesTable::find(_salesId);
real shipping;
;

queryRun = new QueryRun(OSSDlvMode::query(_salesTable.CustAccount, _salesTable, _salesTable.DlvMode,
OCSCatalogueTable::find(OCSBrandTable::find(_salesTable.OSSBrandId).CatalogueId, false).ShippingChargegroupId));

while(queryRun.next())
{
deliveryChargeTable = queryRun.get(tablenum(OSSDeliveryChargeTable));
table = deliveryChargeTable;
if(deliveryChargeTable)
{
_salesTable.ossCreateMarkupTransFromChargeRecord(true, deliveryChargeTable, deliveryChargeTable.DlvModeId);
}
}
shipping = MarkupTrans::ossCalculateDeliveryCharges(_salesTable);

return shipping;
}

Getting tax(multiple ways):

displayFields = SalesTotals::displayFieldsServer(SalesTable::find(oldSalesId), SalesUpdate::All, ‘USD’);

tax = conPeek(displayFields, 16);
ossOrderValue.Tax = Tax;
if(!ossOrderValue.Tax)
ossOrderValue.Tax = SalesTable::find(oldSalesId).ossTaxCalc();
if(!ossOrderValue.Tax)
{
while select salesLineTax
where salesLineTax.SalesId == oldSalesId
{
tax += Tax::calcTaxAmount(salesLineTax.TaxGroup, salesLineTax.TaxItemGroup, Systemdateget(), salesLineTax.CurrencyCode,
salesLineTax.LineAmount + ossOrderValue.ShippingCharges, TaxModuleType::Sales);
}

ossOrderValue.Tax = tax;
}