I'm trying to perform PO line registration and referring to these links:
https://domhk.blogspot.com/2013/01/ax2012-update-invent-registration-from.html
https://sangeethwiki.blogspot.com/2020/11/po-registration-x-d365fo.html
Here's my button event handler codes:
[FormControlEventHandler(formControlStr(PurchTable, BtnTestRegister), FormControlEventType::Clicked)]
public static void BtnTestRegister_OnClicked(FormControl sender, FormControlEventArgs e)
{
FormRun formRun = sender.formRun();
FormDataSource PurchTable_ds = formRun.dataSource("PurchTable");
PurchTable purchTable = PurchTable_ds.cursor();
PurchLine purchLine;
InventTrans inventTrans;
InventTransOrigin inventTransOrigin;
InventDim inventDim;
InventTransWMS_Register inventTransWMS_register;
TmpInventTransWMS tmpInventTransWMS;
////USMF Sample PO = '00000600' ItemId = 'M9010batch'
InventSiteId _inventSiteId = '1';
InventLocationId _inventLocationId = '11';
WMSLocationId _wmsLocationId = "11";
InventBatchSerialId _batchNo = "GH34567";
InventBatchSerialId _serialNo = "";
InventSite inventSite; //Site
InventLocation inventLocation; //Warehouse
WMSLocation wmsLocation; //Location
InventBatch inventBatch; //Batch number
InventSerial inventSerial; //Serial number
;
ttsbegin;
while select RecId, InventTransId, ItemId, PurchId from purchLine
where purchLine.PurchId == purchTable.PurchId
&& purchLine.IsDeleted == NoYes::No
&& purchLine.PurchQty > 0
{
inventTrans = InventTrans::findTransId(purchLine.InventTransId);
if(inventTrans && inventTrans.StatusReceipt != StatusReceipt::Registered)
{
inventDim = inventTrans.inventDim();
tmpInventTransWMS.clear();
tmpInventTransWMS.initFromInventTrans(inventTrans);
tmpInventTransWMS.InventQty = inventTrans.Qty;
if(_wmsLocationId)
{
wmsLocation = WMSLocation::find(_wmsLocationId, _inventLocationId);
if(wmsLocation)
inventDim.wMSLocationId = wmsLocation.wMSLocationId;
else
error(strFmt('Location %1 not exist in warehouse %1.', _wmsLocationId, _inventLocationId));
}
if(_batchNo)
{
inventBatch = InventBatch::findOrCreate(_batchNo, inventTrans.ItemId);
inventDim.inventBatchId = inventBatch.inventBatchId;
}
if(_serialNo)
{
inventSerial = InventSerial::findOrCreate(_serialNo, inventTrans.ItemId);
inventDim.inventSerialId = inventSerial.InventSerialId;
}
tmpInventTransWMS.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;
tmpInventTransWMS.insert();
inventTransWMS_register = inventTransWMS_register::newStandard(tmpInventTransWMS);
inventTransWMS_Register.createFromInventTrans(inventTrans, inventDim);
inventTransWMS_register.writeTmpInventTransWMS(tmpInventTransWMS, inventTrans, inventTrans.inventDim());
inventTransWMS_register.updateInvent(purchLine);
Info(strFmt('%1 of item %2 in PurchId %3 registered with Batch: %4 Location: %5',
tmpInventTransWMS.InventQty,
purchLine.ItemId,
purchLine.PurchId, _batchNo, _wmsLocationId));
}
}
ttscommit;
}
The item tracking dimension for Item "M9010batch" is until batch number.
In the code, we select PO line, update InventDimId with batch number and insert tmpInventTransWMS with updated InvendDimId, then call inventTransWMS_register.updateInvent.
Running this code, I get error message "Inventory dimension Batch number must be specified. "
Debugging the code, even though we pass tmpInventTransWMS with the updated InventDimId, inside method InventTransWMS_Register::updateInventFromMovementServer, the tmpInventTransWMS.InventDimId is somehow being reset.
Naturally, this is not the case when we perform the action manually via InventTransRegister form.
I'm still trying to compare debugging results between our code and standard form calling, but if anyone can point out what I passed wrongly, are highly appreciated.
Thank you.