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;
}

Advertisements

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;
}

Wokflow approver name and date time

HI, Below code used to get workflow approve name based on purchase order Id or RecId.

select firstonly workflowTrackingStatusTable
order by RecId desc
where workflowTrackingStatusTable.ContextTableId == tableNum(PurchTable)
&& workflowTrackingStatusTable.ContextRecId == purchTable.RecId
&& workflowTrackingStatusTable.TrackingStatus == WorkflowTrackingStatus::Completed;
select * from workflowTrackingTable
order by CreatedDateTime desc, RecId desc
where workflowTrackingTable.WorkflowTrackingStatusTable == workflowTrackingStatusTable.RecId
&& workflowTrackingTable.TrackingContext == WorkflowTrackingContext::WorkItem
&& workflowTrackingTable.TrackingType == WorkflowTrackingType::Approval;

select firstOnly dirPersonUser
order by ValidTo desc
where dirPersonUser.User == workflowTrackingTable.User;

select dirPerson
where dirPerson.RecId == dirPersonUser.PersonParty
join hcmWorker
where hcmWorker.Person == dirPerson.RecId;

select hcmPositionWorkerAssignment
order by ValidTo desc
where hcmPositionWorkerAssignment.Worker == hcmWorker.RecId
join hcmPosition
where hcmPosition.RecId == hcmPositionWorkerAssignment.Position;

name = dirPerson.Name;

select * from workflowTrackingCommentTable where workflowTrackingCommentTable.WorkflowTrackingTable == workflowTrackingTable.RecId;

approvedBy = strFmt(“@SYS3492” + ‘ < %1, %2 – %3 >’, name,
hcmPosition.description(),
DateTimeUtil::applyTimeZoneOffset(workflowTrackingCommentTable.createdDateTime, DateTimeUtil::getCompanyTimeZone()));

notexists Join in X++

HI , Below sample used to get data from same table using 2(A,B) buffer’s. ¬†Ex: Should get data from A which doesn’t have records in B based on catalog condition.

 

while select priceDiscTableSource

where priceDiscTableSource.OCSCatalogueRelation == sourceCatalog
notexists join priceDiscTableDestination
where priceDiscTableSource.ItemRelation == priceDiscTableDestination.ItemRelation
&& priceDiscTableDestination.OCSCatalogueRelation == diaCatalogDestination

Build query range on Multiple Tables

str qvalue;

qvalue = strFmt(‘((%1 like “%5”) || (%2 like “%5”) || (%3 like “%5”) || (%4 like “%5”) || (%6 like “%5”) || (%7 like “%5”) || (%8 like “%5”))’,
fieldStr(ProductsAndVariants, ItemId),
fieldStr(ProductsAndVariants, ItemName),
fieldStr(ProductsAndVariants, VariantDescription),
fieldStr(ProductsAndVariants, ItemSearchName),
_searchText,
fieldStr(CustVendExternalItem, ExternalItemId ),
fieldStr(CustVendExternalItem, ExternalItemTxt),
fieldStr(InventItemBarcode, itemBarCode));

 

qbs = query.addDataSource(tableNum(ProductAndVariantSearch));
qbr = qbs.addRange(fieldNum(ProductAndVariantSearch, DataAreaId));
qbr.value(qvalue);

 

Auto build query based on query

Apply the ranges from query :

 

public void executeQuery()
{
if(call)
{
if(element.args().caller())
{
q = inventLookup.getQuery();
for(i = 1; i <= q.dataSourceCount(); i++)
{
rangeCount = q.dataSourceNo(i).rangeCount();
for(j = 1; j <= rangeCount; j++)
{
QBR = q.dataSourceNo(i).range(j);
dictField = new DictField(QBR.table(),fieldname2id(QBR.table(),QBR.name()));
fieldLabel = dictField.label();
if(QBR.value())
{
ProductAndVariantSearch_q.dataSourceTable(QBR.table()).addRange(fieldname2id(QBR.table(),QBR.name())).value(QBR.value());
}
}
}
}
}

super();
}