How to: Explode BOM through X++

Posted on Updated on

Sorry about the delay, to compensate it I will be posting twice this week.

1.Create a new Temporary Table with the following properties

Propertie Value
Name TmpBOMExplode
TableType InMemory
Field Extended Data Type
BOMQty BOMQty
ItemId ItemId
ItemName ItemName
Level Int
RefItemID ItemId

It should look like this:

TmpBOMExplode

2.Create a new Class, I will call it BOMDesign, and Modify classDeclaration.

class BOMDesign
{
    TmpBOMExplode tmpBOM;
}

3.Create a method to create the parent Item.

private int InsertParentItem(ItemId _itemId, int _level)
{
    InventTable inventTable;
    ;

    //Gets the parent information and then insert on TmpBOMExplode Table with level 0.
    select firstOnly inventTable where inventTable.ItemId == _itemId;

    tmpBOM.ItemId = _itemId;
    tmpBOM.ItemName = inventTable.itemName();
    tmpBOM.Level = _level;
    tmpBOM.BOMQty = 1 ;
    tmpBOM.insert();

    return _level+ 1 ;
}

4.Create a method that verifies if the current Item is also a BOM Item.

boolean hasChild(ItemId _itemId)
{
    BOMVersion  bomVersion;
    ;

    //Check if the item is also a BOM item.
    select firstonly bomVersion
            where bomVersion.ItemId == _itemid
            && bomVersion.Active
            && bomVersion.FromDate <= systemdateget ()
            && (!bomVersion.ToDate || bomVersion.ToDate >= systemdateget ());

    if (bomVersion.RecId)
        return true ;

    return false ;
}

5.Now, create the method which will explode the Item recursively, every time it calls itself we have to increase the level number.
The Level number is useful if you want to create a Tree Control later.

void itemExplode(ItemId _ItemId, int _level = 0, BOMQty _bomQty = 1)
{
    BOM         bomTable;
    InventTable inventTable;
    BOMVersion  bomVersion;
    ;

    //Insert parent Item
    if (_level == 0)
        _level = this.InsertParentItem(_ItemId, _level);

    //Verifies if the Item exists in BOMVersion Table.
    //The item must be active and not expired.
    select firstonly bomVersion
            where bomVersion.ItemId == _itemid
            && bomVersion.Active
            && bomVersion.FromDate <= systemdateget()
            && (!bomVersion.ToDate || bomVersion.ToDate >= systemdateget());

    if (bomVersion.RecId)
    {
        //Every item on BOMVersion has a BOMId which is used to show
        //which products belong to a BOM Item.
        While select bomTable
            where bomTable.BOMId == bomVersion.BOMId
            join inventTable
            where bomTable.ItemId == inventTable.ItemId
        {
            //Insert the items that compose the BOM Item with level 1.
            tmpBOM.ItemId = bomTable.ItemId;
            tmpBOM.ItemName = inventTable.itemName();
            tmpBOM.RefItemId = bomVersion.ItemId;
            tmpBOM.BOMQty = bomTable.BOMQty / bomTable.BOMQtySerie * _bomQty;
            tmpBOM.Level = _level;
            tmpBOM.insert();

            //This method is used to check if the BOM Item is composed by
            //another BOM Item, case true it will call the method recursively
            // with level 2.
            if (this.hasChild(bomTable.ItemId))
                this.itemExplode(bomTable.ItemId, _level+ 1, tmpBOM.BOMQty);
        }
    }
}

5.Main method used to test Class.

public static void main(Args args)
{
    BOMDesign bomDesign = new BOMDesign();
    ;

    bomDesign.itemExplode( 'D0001', 0 );
}

6.I have temporarily changed the TableType to regular so I can see the results.

TmpBOMExplodeTable

Advertisements

5 thoughts on “How to: Explode BOM through X++

    krishna said:
    August 30, 2015 at 11:29 am

    Hi,
    i wont think so it worked out.
    have you tested your code in different scenarios .
    please give a reply to me for further discussion.

    Bijan Ghofranian said:
    October 9, 2015 at 12:55 am

    Great example of recursion used in a very applicable scenario.
    Thanks – Bijan

    Metin Emre said:
    November 8, 2017 at 9:30 am

    Thank you for solution. You doesn’t consider unit conversion. I included unit conversion to your code: http://metinemreenglish.blogspot.com.tr/2017/11/ax-2012-bom-explode.html

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s