Following job will create the link purchase order for sales order. I have created this job in AX 2009 and I am assuming that it will also work with AX 2012

 

//Create the Purchase order from Sales order
static void createLinkPurchaseOrder_Demo(Args _args)
{
PurchAutoCreate purchAutoCreate;
TmpPurchLinePrice tplP;
VendAccount prevVendAccount;
SalesTable salesTableLocal;
SalesLine salesLineLocal;
InventTable inventTable;
LineNum lineNumber = 0;
PurchCreateFromSalesOrder purchCreateFromSalesOrder;
;

try
{
ttsbegin;
while select salesLineLocal
order by inventTable.PrimaryVendorId
where salesLineLocal.SalesId == ‘00018_036’
join inventTable
where inventTable.ItemId == salesLineLocal.ItemId
{
if (prevVendAccount && prevVendAccount != inventTable.PrimaryVendorId)
{
purchAutoCreate = PurchAutoCreate::construct(tplP, purchCreateFromSalesOrder);
purchAutoCreate.create();
delete_from tplP;
lineNumber = 0;
}

salesTableLocal = salesLineLocal.salesTable();
purchCreateFromSalesOrder = PurchCreateFromSalesOrder::construct();
purchCreateFromSalesOrder.callerRecord(salesTableLocal);

tplP.clear();
lineNumber += 1;
tplP.SalesId = salesLineLocal.SalesId;
tplP.LineNum = lineNumber;
tplP.SalesLineRefRecId = salesLineLocal.RecId;
tplP.AccountNum = inventTable.PrimaryVendorId;

tplP.ItemId = salesLineLocal.ItemId;
tplP.InventDimId = salesLineLocal.InventDimId;

tplP.Included = NoYes::Yes;

tplP.PurchQty = salesLineLocal.SalesQty;
tplP.QtyOrdered = salesLineLocal.QtyOrdered;

tplP.PurchUnit = salesLineLocal.SalesUnit;
tplP.LineAmount = salesLineLocal.LineAmount;

tplP.LineDisc = salesLineLocal.LineDisc;
tplP.LinePercent = salesLineLocal.LinePercent;
tplP.MultiLineDisc = salesLineLocal.MultiLnDisc;
tplP.MultiLinePercent = salesLineLocal.MultiLnPercent;

tplP.Price = salesLineLocal.SalesPrice;
tplP.PriceUnit = salesLineLocal.PriceUnit;
tplP.CurrencyCode = salesLineLocal.CurrencyCode;

tplP.Markup = salesLineLocal.SalesMarkup;

tplP.insert();

prevVendAccount = tplP.AccountNum;
}

if (prevVendAccount)
{
purchAutoCreate = PurchAutoCreate::construct(tplP, purchCreateFromSalesOrder);
purchAutoCreate.create();
}
ttscommit;
}
catch (Exception::Error)
{
ttsabort;
}
}

Leave a Reply

Recent Comments

    Archives

    Categories