I have created a class which read the xml ,pick the voucher no and create the Invoice approval journal and also post it.
As I am beginner in AX ,any suggestion to improve the quality of code will be highly appreciated.
Follow the below steps
Method 1-
class CreateInvoiceApprovalJournalBatch
{
#define.node('binayxml')
XmlDocument xmlDocument;
XmlNodeList lineList;
XmlNode lineRow;
int cnt;
int64 nRecId;
str fileName,pfilename;
Voucher voucher;
VendTrans vendTrans;
AmountCurDebit amountCurDebit;
AmountCurCredit amountCurCredit;
AmountCurCredit total;
Approved approved;
LedgerJournalACType acttype;
LedgerJournalCheckPost ledgerJournalCheckPost;
LedgerJournalTable ledgerJournalTable;
LedgerJournalNameIdDaily JOurnalnum;
LedgerJournalType ledgerJournalType;
LedgerJournalTrans ledgerJournalTrans,_ledgerJournalTrans;
FileIoPermission _perm;
//MoveFileName
#Evat_NL
#File
// variable declaration to retrieve files from folder
Filename ProcessedFolder;
container filesS;
int i;
str charHolder;
}
Mehod-2
public container findMatchingFiles(str _folderPath , str _filePattern = '*.*')
{
System.IO.DirectoryInfo directory;
System.IO.FileInfo[] files;
System.IO.FileInfo file;
InteropPermission permission;
counter filesCount;
counter loop;
container mathchingFiles;
;
permission = new InteropPermission(InteropKind::ClrInterop);
permission.assert();
directory = new System.IO.DirectoryInfo(_folderPath);
files = directory.GetFiles(_filePattern);
filesCount = files.get_Length();
for (loop = 0; loop < filesCount; loop++)
{
file = files.GetValue(loop);
fileName = file.get_FullName();
mathchingFiles = conins(mathchingFiles, conlen(mathchingFiles) + 1, fileName);
}
CodeAccessPermission::revertAssert();
return mathchingFiles;
}
Method3:-
[SysEntryPointAttribute(false)]
public void processRecords()
{
amountCurCredit = 0;
amountCurDebit = 0;
//XML FILE LOCATION
filesS = this.findMatchingFiles(@"\\xyz\BINAY\Source");//Source File location
ProcessedFolder = @"\\abc\\ProcessedXmlFile\";//Processed File Location -
//Destination
try
{
for (i = 1; i <= conlen(filesS); i++)
{
fileName = conpeek(filesS, i);
_perm = new FileIoPermission(fileName,'r');
_perm.assert();
info(fileName);
//Read XML CODE
xmlDocument = xmlDocument::newFile(fileName);
lineList = xmlDocument.getElementsByTagName('axLGJ:LedgerJournalTrans');
cnt = lineList.length();
lineRow = lineList.nextNode();
while (lineRow)
{
voucher = lineRow.getNamedElement(binay:Voucher').text();
if(!voucher)
{
throw Global::error("Invalid Voucher No - %1");
}
lineRow = lineList.nextNode();
}
//Invoice Approval Header Creation
ledgerJournalTable.initValue();
ledgerJournalTable.Name = 'X++ Job ROws ';
ledgerJournalTable.JournalName = 'AP-XXXX';//Journal Name
ledgerJournalTable.JournalType =ledgerJournalType::Approval;
ledgerJournalTable.JournalNum = JournalTableData::newTable(ledgerJournalTable).nextJournalId();
//ledgerJournalTable Insertion
ledgerJournalTable.insert();
//Create Lines
Select firstOnly ledgerJournalTrans
where ledgerJournalTrans.Voucher == voucher;
ledgerJournalTable = LedgerJournalTable::find(ledgerJournalTable.JournalNum);
ledgerJournalTrans = LedgerJournalTrans::findRecId(ledgerJournalTrans.RecId, false);
//Create 3 Lines
LedgerJournalEngine_Server::addVoucher(ledgerJournalTable, ledgerJournalTrans);
select firstOnly voucher from vendTrans
where vendTrans.Voucher == voucher;
ledgerJournalTrans.VendTransId = vendTrans.RecId;
Select sum(AmountCurCredit),sum(AmountCurDebit) from ledgerJournalTrans
where ledgerJournalTrans.Voucher == voucher
&& ledgerJournalTrans.AccountType == LedgerJournalACType::Ledger
&& ledgerJournalTrans.Approved == NoYes::Yes;
if(ledgerJournalTrans)
{
amountCurCredit = ledgerJournalTrans.AmountCurCredit;
amountCurDebit = ledgerJournalTrans.AmountCurDebit;
total = abs(amountCurDebit - amountCurCredit);
}
ledgerJournalTrans.clear();
select * from ledgerJournalTrans
where ledgerJournalTrans.Voucher == voucher
&& ledgerJournalTrans.Approved == NoYes::Yes
&& ledgerJournalTrans.AccountType == LedgerJournalACType::Ledger
&& ledgerJournalTrans.AmountCurCredit == total;
//Insert 4th Ledger Journal Trans Line
if (ledgerJournalTrans)
{
ttsbegin;
_ledgerJournalTrans.initValue();
_ledgerJournalTrans.Txt = ledgerJournalTrans.Txt;
_ledgerJournalTrans.TransDate = ledgerJournalTrans.TransDate;
_ledgerJournalTrans.CurrencyCode = ledgerJournalTrans.CurrencyCode;
_ledgerJournalTrans.AmountCurDebit = total;
_ledgerJournalTrans.Voucher = ledgerJournalTrans.Voucher;
_ledgerJournalTrans.AccountType = ledgerJournalTrans.AccountType;
_ledgerJournalTrans.Approved = ledgerJournalTrans.Approved;
_ledgerJournalTrans.Approver = ledgerJournalTrans.Approver;
_ledgerJournalTrans.Company = ledgerJournalTrans.Company;
_ledgerJournalTrans.CurrencyCode = ledgerJournalTrans.CurrencyCode;
_ledgerJournalTrans.CashDiscCode = ledgerJournalTrans.CashDiscCode;
_ledgerJournalTrans.DateCashDisc = ledgerJournalTrans.DateCashDisc;
_ledgerJournalTrans.DocumentDate = ledgerJournalTrans.DocumentDate;
_ledgerJournalTrans.DocumentNum = ledgerJournalTrans.DocumentNum;
_ledgerJournalTrans.Due = ledgerJournalTrans.Due;
_ledgerJournalTrans.ForeignCompany = ledgerJournalTrans.ForeignCompany;
_ledgerJournalTrans.Invoice = ledgerJournalTrans.Invoice;
_ledgerJournalTrans.vatNumJournal = ledgerJournalTrans.vatNumJournal;
_ledgerJournalTrans.JournalNum = ledgerJournalTrans.JournalNum;
_ledgerJournalTrans.Payment = ledgerJournalTrans.Payment;
_ledgerJournalTrans.PaymentAccount = ledgerJournalTrans.PaymentAccount;
_ledgerJournalTrans.PaymentNotes = ledgerJournalTrans.PaymentNotes;
_ledgerJournalTrans.PaymReference = ledgerJournalTrans.PaymReference;
_ledgerJournalTrans.PaymentStatus = ledgerJournalTrans.PaymentStatus;
_ledgerJournalTrans.PaymId = ledgerJournalTrans.PaymId;
_ledgerJournalTrans.PaymMode = ledgerJournalTrans.PaymMode;
_ledgerJournalTrans.PaymSpec = ledgerJournalTrans.PaymSpec;
_ledgerJournalTrans.PostingProfile = ledgerJournalTrans.PostingProfile;
_ledgerJournalTrans.SettleVoucher = ledgerJournalTrans.SettleVoucher;
_ledgerJournalTrans.PurchLedgerPosting = ArrivalPostingType::None;
_ledgerJournalTrans.Invisible = NoYes::No;
_ledgerJournalTrans.NoEdit = NoYes::No;
_ledgerJournalTrans.TransactionType = ledgerJournalTrans.TransactionType;
_ledgerJournalTrans.LedgerDimension = ledgerJournalTrans.LedgerDimension;
_ledgerJournalTrans.ExchRate = ledgerJournalTrans.ExchRate;
_ledgerJournalTrans.ExchRateSecond = ledgerJournalTrans.ExchRateSecond;
_ledgerJournalTrans.Triangulation = ledgerJournalTrans.Triangulation;
_ledgerJournalTrans.VendTransId = vendTrans.RecId;
_ledgerJournalTrans.insert();
//Posting Ledger Journal Trans
ledgerJournalCheckPost = ledgerJournalCheckPost::newLedgerJournalTable(ledgerJournalTable,NoYes::Yes);
ledgerJournalCheckPost.run();
pfilename = ProcessedFolder + Voucher +'.xml';
//Move file after processing
System.IO.File::Move(fileName,pfilename);
ttsCommit;
}
}
}
catch(Exception::Error)
{
info("Caught 'Exception::Error'.");
}
}
Add the class in the Action Menu Item ,to make it batch executable do below steps
Action/MenuItem /Properties
Object Type - Class
Object -SysOperationServiceController
Parameters-ClassName.method