In this blog, we will see how to create the transfer order through X++ code in D365FnO and AX 2012.
In Microsoft Dynamics 365 for Finance and Operations (D365FnO) and Microsoft Dynamics AX 2012, a transfer order is a document that allows you to move items from one location to another within your warehouse or between warehouses. To create a transfer order using X++ in D365FnO, you can follow the following code:
Create new class.
- First create the Transfer order header record inserting data into InventTransferTable
- Import the transfer order lines data into InventTransferLine
class CreateTransferOrderJournal
{
MenuItemNameAction menuItemNameAction;
public static void main(Args _args)
{
CreateTransferOrderJournal transferOrderJournal = CreateTransferOrderJournal::construct();
// parm method to get and store menu item name
transferOrderJournal.parmMenuItemNameAction(_args.menuItemName());
// Check the class is running from correct data-source
if (_args.dataset() == tableNum(InventTransferTable))
{
// Check the running menu-item name
if (transferOrderJournal.parmMenuItemNameAction() == menuitemActionStr(ImportFBAJournalLines))
{
// Call the run method to execute the logic
transferOrderJournal.run(_args);
// Call the refreshCallerForm method to refresh the form after data-insertion
transferOrderJournal.refreshCallerForm(_args);
}
}
}
public MenuItemNameAction parmMenuItemNameAction(MenuItemNameAction _menuItemNameAction = menuItemNameAction)
{
menuItemNameAction = _menuItemNameAction;
return menuItemNameAction;
}
public void run(Args _args)
{
InventTransferTable inventTransferTable;
// Create the Transfer-Order journal header
inventTransferTable = CreateTransferOrderJournal::CreateTransferJournalHeader();
if(inventTransferTable.RecId != 0)
{
str strMessage = strFmt("Do you want to import lines against %1.", inventTransferTable.FBAJournalId);
str strTitle = "Import lines";
if(Box::yesNo(strMessage, DialogButton::No, strTitle) == DialogButton::Yes)
{
try
{
ttsBegin;
// Call the importLines method to import the lines and papulate into journal
this.importLines(inventTransferTable);
ttsCommit;
}
catch
{
ttsAbort;
infolog.add(Exception::Error, strFmt("Import has been interrupted"));
}
}
}
}
// Method for Transfer-Order header
private static InventTransferTable CreateTransferJournalHeader()
{
InventTransferTable inventTransferTable;
NumberSequenceReference numberSequenceReference;
NumberSeq numberSeq;
inventTransferTable.clear();
inventTransferTable.initValue();
numberSequenceReference = InventParameters::numRefTransferId();
numberSeq = numberSeq::newGetNumFromCode(numberSequenceReference.numberSequenceTable().NumberSequence);
inventTransferTable.TransferId = numberSeq.num();
inventTransferTable.InventLocationIdFrom = '11';
inventTransferTable.modifiedField(fieldNum(InventTransferTable, InventLocationIdFrom));
inventTransferTable.InventLocationIdTo = '13';
inventTransferTable.modifiedField(fieldNum(InventTransferTable, InventLocationIdTo));
inventTransferTable.TransferStatus = InventTransferStatus::Created;
inventTransferTable.insert();
return inventTransferTable;
}
// Method for Transfer-Order lines
private void importLines(InventTransferTable _inventTransferTable)
{
FBAJournalLines FBAJournalLines;
InventTransferLine inventTransferLine;
InventDim inventDim;
while select FBAJournalLines
where FBAJournalLines.FBAJournalId == _inventTransferTable.FBAJournalId
&& _inventTransferTable.IsFBAJournal == NoYes::Yes
{
if(FBAJournalLines.RecId != 0)
{
inventTransferLine.clear();
inventTransferLine.initFromInventTransferTable(_inventTransferTable, NoYes::Yes);
inventTransferLine.ItemId = FBAJournalLines.ItemId;
inventTransferLine.initFromInventTable(InventTable::find(FBAJournalLines.ItemId));
inventTransFerLine.LineNum = InventTransferLine::lastLineNum(_inventTransferTable.TransferId) + 1;
inventTransFerLine.ItemAsinId = FBAJournalLines.ItemAsinId;
inventTransFerLine.AsinIdDescription = FBAJournalLines.AsinIdDescription;
inventTransferLine.QtyTransfer = FBAJournalLines.Qty;
inventTransferLine.QtyRemainReceive = FBAJournalLines.Qty;
inventTransferLine.QtyRemainShip = FBAJournalLines.Qty;
inventTransferLine.QtyShipNow = 0;
inventTransferLine.QtyReceiveNow = 0;
inventDim = inventTransferLine.inventDim();
inventDim.InventSizeId = inventDim::find(FBAJournalLines.InventDimId).InventSizeId;
inventDim.InventColorId = inventDim::find(FBAJournalLines.InventDimId).InventColorId;
inventDim.InventStyleId = inventDim::find(FBAJournalLines.InventDimId).InventStyleId;
inventDim.inventSerialId = inventDim::find(FBAJournalLines.InventDimId).inventSerialId;
inventDim.InventSiteId = InventLocation::find(_inventTransferTable.InventLocationIdFrom).InventSiteId;
inventDim.InventLocationId = _inventTransferTable.InventLocationIdFrom;
inventTransferLine.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;
inventTransferLine.insert();
}
else
{
Info("No line exists.");
}
}
}
public void refreshCallerForm(Args _args)
{
_args.callerFormControl().formRun().dataSource().reread();
_args.callerFormControl().formRun().dataSource().refresh();
_args.callerFormControl().formRun().dataSource().research(true);
}
public static CreateTransferOrderJournal construct()
{
return new CreateTransferOrderJournal();
}
}