(SOLVED) Dynamics AX 2009 – Receive Purchase Order Line using x++ & .Net Business Connector

We needed to expose a WCF service so that our logistics vendors could execute a purchase order
line receipt. There were a lot of results returned from a google & yahoo search but a good portion
of them weren’t very specific. After hours of trial and error I came up with the x++ code below
that includes a serial number update through the Inventory Dimension.
public static void receivePurchLine(PackingSlipId _packSlipId, PurchId _purchId,
Qty _qty, ItemId _itemId, LineNum _lineNum, str _inventLocation)
{
PurchFormLetter purchFormLetter;
ParmId parmId;
PurchParmTable purchParmTable;
PurchParmLine purchParmLine;
PurchTable purchTable;
PurchLine purchLine;
InventDim currentInventDim;
InventDim updatedInventDim;
;
purchTable = PurchTable::find(_purchId);
purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);
purchFormLetter.createParmUpdate();
purchFormLetter.createParmTable(purchParmTable, purchTable);
purchParmTable.Num = _packSlipId;
purchParmTable.insert();
while select purchLine
where purchLine.PurchId == purchTable.PurchId
{
purchParmLine.InitFromPurchLine(purchLine);
purchParmLine.ParmId = purchParmTable.ParmId;
purchParmLine.TableRefId = purchParmTable.TableRefId;
if (purchLine.ItemId == _itemId && purchLine.LineNum == _lineNum)
{
currentInventDim = InventDim::find(purchLine.InventDimId);
if (currentInventDim.RecId)
{
updatedInventDim =
InventDim::findOrCreateOwnCondDispSiteLocation(currentInventDim.configId,
currentInventDim.InventSizeId,
currentInventDim.InventColorId,
currentInventDim.InventSiteId,
_inventLocation);
if (!updatedInventDim.RecId)
{
throw error('Unable to find or create destination inventory dimension');
}
}
purchParmLine.ReceiveNow = _qty;
purchParmLine.RemainAfter = purchLine.QtyOrdered - _qty;
purchParmLine.InventDimId = updatedInventDim.inventDimId;
}
else
{
purchParmLine.ReceiveNow = 0;
purchParmLine.RemainAfter = purchLine.QtyOrdered;
}
purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);
purchParmLine.setLineAmount();
purchParmLine.insert();
}
purchFormLetter.proforma (false); // proforma ?
purchFormLetter.printFormLetter(false); // print ?
purchFormLetter.specQty (PurchUpdate::All); // what to update?
purchFormLetter.transDate (today()); // update date
purchFormLetter.run();
}