Create and Post Free Text Invoice in D365 for Operations
I've seen a bunch of articles that discuss how to create and post a free text invoice in Ax2012. I decided to write my own version as, some things might be different between the 2 versions. This article is based on the example found at: http://dynamicsaxthehardway.blogspot.nl/2015/04/import-free-text-invoice-using-x.html
Creation of CustInvoiceTable
This has remained practically the same:
custTable = CustTable::find("yourcustomerid"); custInvoiceTable.initFromCustTable(custTable); custInvoiceTable.insert();
Creation of CustInvoiceLine
There is a major difference here in how LedgerDimension is set up. Previously you'd use AxDimensionUtil::getLedgerAccountId. But this method does not exist anymore in D365. I used LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId.
In the example below I'm also using the RandomGenerate feature to generate random unit prices
custInvoiceLine.clear(); custInvoiceLine.initValue(); custInvoiceLine.initFromCustInvoiceTable(custInvoiceTable); custInvoiceLine.LedgerDimension = LedgerDefaultAccountHelper::getDefaultAccountFromMainAccountId(mainAccountId); custInvoiceLine.UnitPrice = new RandomGenerate().randomInt(1,200); custInvoiceLine.AmountCur = custInvoiceLine.UnitPrice; custInvoiceLine.Description = 'FreeTxIv' + int2str(i); custInvoiceLine.TaxItemGroup = 'full'; custInvoiceLine.ParentRecId = custInvoiceTable.RecId; custInvoiceLine.LineNum = 1; //or a sequence of numbers if you have more custInvoiceLine.insert();
Posting the invoice
Posting the invoice has also remained roughly the same
custPostInvoice = new CustPostInvoice(custInvoiceTable); custPostInvoice.run();
However there is one trick that is not very well documented. If exactly after you post you want to read your subledgervouchers you need to enable synchronous voucher posting.
By standard, Microsoft makes subledger postings work using a batch job. This can be set up on GL\Periodic Tasks\Batch transfer for subledger journals. In the demo companies it runs once every 10 minutes. You can however set it to synchronous by either:
a) changing it under GL Parameters\Batch Transfer Rules
b) locating the appropriate record in subledgerjournaltransferrule and setting transfermode to Synchronous ex;
update_recordset subledgerJournaltransferrule setting transfermode = SubLedgerJournalTransferMode::Synchronous;
*This post is locked for comments