Create Purchase Order X++ and link matched Purchase Agreement
Assume that ATOSMRTable hold information for the items lines that required to create purchase order lines
ATOSMRLine hold item information and matched agreement line
public void PurchProcess(ATOSMRId _mrId) { ATOSMRTable mrTable; ATOSMRLine linesGroupByAgreement,lines; PurchAgreementHeader purchAgreementHeader; PurchTable purchTable; PurchLine purchLine; VendTable vendTable ; AxPurchTable axPurchTable; AxPurchLine axPurchLine; select mrTable where mrTable.ATOSMRId == _mrId && mrTable.ATOSMRStatus == ATOSMRStatusBase::Approved; setPrefix(strFmt('Purchase Processing')); while select linesGroupByAgreement group by linesGroupByAgreement.PurchAgreementId where linesGroupByAgreement.ATOSMRId == mrTable.ATOSMRId && linesGroupByAgreement.ProcessType == ATOSMRLineProcessTypeBase::PurchaseOrder && linesGroupByAgreement.LineStatus == ATOSMRLineStatusBase::Approved { purchAgreementHeader = purchAgreementHeader::findAgreementId( linesGroupByAgreement.PurchAgreementId); vendTable = vendTable::find(purchAgreementHeader.VendAccount); purchTable.clear(); purchTable.initFromVendTable(vendTable); axPurchTable = axPurchTable::newPurchTable(purchTable); axPurchTable.parmMatchingAgreement(purchAgreementHeader.RecId); axPurchTable.parmPurchaseType(PurchaseType::Purch); axPurchTable.parmDocumentStatus(DocumentStatus::PurchaseOrder); axPurchTable.parmAccountingDate(systemDateGet()); //axPurchTable.parmDeliveryDate(016\2012); axPurchTable.parmPurchStatus(PurchStatus::Backorder); axPurchTable.parmInventSiteId(InventLocation::find(mrTable.InventLocationId).InventSiteId); axPurchTable.parmInventLocationId(mrTable.InventLocationId); axPurchTable.doSave(); if(axPurchTable.purchTable()) { setPrefix(strFmt('Purchase Order #%1',axPurchTable.purchTable().PurchId)); while select lines where lines.PurchAgreementId == linesGroupByAgreement.PurchAgreementId && lines.ATOSMRId == _mrId && lines.ProcessType == ATOSMRLineProcessTypeBase::PurchaseOrder && lines.LineStatus == ATOSMRLineStatusBase::Approved { try { purchLine.clear(); purchLine.initFromPurchTable(purchTable); axPurchLine = AxPurchLine::newPurchLine(purchLine); axpurchLine.parmItemId(lines.ItemId); axPurchLine.parmPurchQty(lines.ApprovedQty); axPurchLine.parmMatchingAgreementLine(lines.MatchingAgreementLine); axPurchLine.doSave(); info(strFmt('Item #%1 Name(%2)',axPurchLine.purchLine().ItemId, axPurchLine.purchLine().itemName())); ATOSMRLine::SetPurchId(lines.RecId,purchTable.PurchId); } catch(Exception::Error) { error(strFmt('Failed creating Item #%1 Name(%2)',axPurchLine.purchLine().ItemId, axPurchLine.purchLine().itemName())); } } if(!purchTable.existPurchLine()) { warning(strFmt('Delete Purchase Order #(%1) as it does not have any valid lines',purchTable.PurchId)); purchTable.delete(); } } } }