PurchFormLetter posting invoice lines

Hello,

I am having problems with the code below. When the PurchFormLetter.Run is executed the ‘reArrange’ method is deleting the PurchParmTable that has just been created! The object passed in is a class with ‘parms’ and a set of lines in it, this is information passed from a supplier as their invoice. Any help most appreciated.

Mike

boolean matchOrderInvoice(dbsBTPurchOrderInvoice _invoice)
{

try
{
purchFormLetter = PurchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.getLast();
purchFormLetter.resetParmListCommonCS();
purchFormLetter.transDate(systemdateget());
purchFormLetter.allowEmptyTable(true);
purchFormLetter.purchTable(purchTable);
purchFormLetter.reArrangeNow(true);
purchFormLetter.jsProcessDropShipment(true);

purchFormLetter.parmCallerTable(purchTable);

purchFormLetter.createParmTable(purchParmTable,
purchTable,
purchName,
deliveryName,
deliveryAddress);

purchParmTable.TableRefId = FormLetter::getTableRef();
purchParmTable.VendInvoiceSaveStatus = VendInvoiceSaveStatus::Pending;
purchParmTable.Num = _invoice.parmInvoiceId();
purchParmTable.Hold = NoYes::Yes;
purchParmTable.insert();

purchFormLetter.parmParmTableNum(purchParmTable.Num);
purchFormLetter.sumNum(purchParmTable.Num);
purchFormLetter.specQty(PurchUpdate::ReceiveNow);

purchFormLetter.createParmUpdate(false);

for (i = 1; i <= _invoice.getLinesCount(); i++)
{
dbsBTPurchOrderInvoiceLine = _invoice.getInvoiceLine(i);

purchParmLine.ParmId = purchParmTable.ParmId;
purchParmLine.TableRefId = purchParmTable.TableRefId;

purchParmLine.ItemId = dbsBTPurchOrderInvoiceLine.parmItemId();
purchParmLine.PriceUnit = dbsBTPurchOrderInvoiceLine.parmPriceUnit();

purchParmLine.ReceiveNow = dbsBTPurchOrderInvoiceLine.parmPurchQty();
purchParmLine.RemainBefore = dbsBTPurchOrderInvoiceLine.parmPurchQty();
purchParmLine.InventNow = dbsBTPurchOrderInvoiceLine.parmPurchQty();
purchParmLine.RemainBeforeInvent = dbsBTPurchOrderInvoiceLine.parmPurchQty();

purchParmLine.PurchPrice = dbsBTPurchOrderInvoiceLine.parmPurchPrice();

purchParmLine.LineAmount = dbsBTPurchOrderInvoiceLine.parmLineAmount();

purchParmLine.insert();
}

purchFormLetter.run();

ret = purchParmTable.checkIfMatched();
}
catch
{
ret = false;
}

return ret;

}

The reason for this behaviour is that reArrange() method decides for itself which lines will be posted and which will not, based on purchParmUpdate settings. If You want to define it manually, try inserting purchParmTable.reArrangeNow(false) before running run() method.

I checked Your code and I have to warn You that manually specifying purchParmLine values is tricky and sometimes very dangerous. Been there, done that, believe me, You really want to check Your code before executing it, otherwise You can cause serious damage to the company using AX. From what I see, You aren’t referring to any purchase lines, You also explicitly specify InventNow the same as ReceiveNow, which might not always be correct if the inventory unit is different from the purchase unit of Your item.

The problem in AX is that some business logic during posting is written on the SalesEditLines / PurchEditLines form and You have to copy some of it (check datasource methods and field methods) to Your code to be 100% sure that Your invoice will be posted correctly. Also, use initValue and initFromPurchLine methods from PurchParmLine table as well as check modifiedField table method for additional logic that might be necessary to transfer to Your code. Run many, many various tests before transfering the functionality to the live system.