In this article, we explained how to write a required minimum X++ code in Microsoft Dynamics 365 in order to create BOMs.
Table Name | Table Description |
BOMTable | The BOMTable table is a header of the BOM list and contains a unique BOMId, that will be used by BOM lines to refer to the header. |
BOMVersion | The BOMVersion table contains all the BOM versions. This table connects to the BOMTable table in order to specify the BOM to which the version refers, and it connects to the InventTable table to specify to which item the BOM version is assigned. The BOMVersion table also connects to the InventDim table to specify a site for the BOM version. Additionally, the BOMVersion table stores information about the approval and activation for each BOM version. |
BOM | The BOM table contains the bill of materials lines. A BOM line connects to an item to consume and a BOM version to which the line applies. |
public void createBOM("Your Data Structure as Parameter")
{
BOMTable bomTable;
BOMVersion bomVersion;
BOM bom;
InventDim inventDim;
InventTable inventTable;
InventDim inventDimLine;
InventTable inventTableLine;
try
{
ttsbegin;
str bomName;
str bomId;
bomId = "<The BOM ID - perhaps a number sequence>";
bomName = "<The name of the BOM>";
inventTable = InventTable::find("Item ID of finished good");
// Create BOMTable
bomTable.initValue();
bomTable.initFromInventTable(inventTable);
bomTable.BOMId = bomId;
bomTable.Name = bomName;
bomTable.Approved = true;
bomTable.Approver = HcmWorker::find(HcmWorkerLookup::currentWorker()).RecId;
bomTable.insert();
// Create BOMVersion
bomVersion.initValue();
bomVersion.initFromInventTable(inventTable);
bomVersion.initFromBOMTable(bomTable);
bomVersion.FromDate = systemDateGet();
bomVersion.Approved = true;
bomVersion.Approver = HcmWorker::find(HcmWorkerLookup::currentWorker()).RecId;
bomVersion.Active = true;
bomVersion.Name = bomName;
// Find or Create InventDim
inventDim.clear();
inventDim.initValue();
inventDim.InventColorId = "Color of finished good";
inventDim.InventSizeId = "Size of finished good";
inventDim = InventDim::findOrCreate(inventDim);
bomVersion.InventDimId = inventDim.inventDimId;
bomVersion.insert();
// Create BOM
// You can use enumeration of the components here
inventTableLine = InventTable::find("Item ID of component/raw material");
inventDimLine.clear();
inventDimLine.initValue();
inventDimLine.InventColorId = "Color of component/raw material";
inventDimLine.InventSizeId = "Size of component/raw material";
inventDimLine = InventDim::findOrCreate(inventDimLine);
bom.clear();
bom.initValue();
bom.ItemId = inventTableLine.ItemId;
bom.BOMId = bomVersion.BOMId;
bom.BOMQty = 1.00;
bom.UnitId = InventTable::find(bom.ItemId).bomUnitId();
bom.WrkCtrConsumption = true;
bom.InventDimId = inventDimLine.inventDimId;
bom.LineNum = BOM::nextLineNum(bom.BOMId);
bom.insert();
ttscommit
}
catch
{
throw error("BOM creation failed");
}
}