Quantcast
Channel: Microsoft Dynamics Ax
Viewing all articles
Browse latest Browse all 181

How to: Explode BOM through X++

$
0
0
1.Create a new Temporary Table with the following properties
PropertieValue
NameTmpBOMExplode
TableTypeInMemory
FieldExtended Data Type
BOMQtyBOMQty
ItemIdItemId
ItemNameItemName
LevelInt
RefItemIDItemId
It should look like this:
TmpBOMExplode
2.Create a new Class, I will call it BOMDesign, and Modify classDeclaration.
1
2
3
4
classBOMDesign
{
    TmpBOMExplode tmpBOM;
}
3.Create a method to create the parent Item.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
privateintInsertParentItem(ItemId _itemId, int_level)
{
    InventTable inventTable;
    ;
 
    //Gets the parent information and then insert on TmpBOMExplode Table with level 0.
    selectfirstOnly inventTable whereinventTable.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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
boolean hasChild(ItemId _itemId)
{
    BOMVersion  bomVersion;
    ;
 
    //Check if the item is also a BOM item.
    selectfirstonly bomVersion
            wherebomVersion.ItemId == _itemid
            && bomVersion.Active
            && bomVersion.FromDate <= systemdateget ()
            && (!bomVersion.ToDate || bomVersion.ToDate >= systemdateget ());
 
    if(bomVersion.RecId)
        returntrue;
 
    returnfalse;
}
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
voiditemExplode(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.
    selectfirstonly bomVersion
            wherebomVersion.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 selectbomTable
            wherebomTable.BOMId == bomVersion.BOMId
            joininventTable
            wherebomTable.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.
1
2
3
4
5
6
7
publicstaticvoidmain(Args args)
{
    BOMDesign bomDesign = newBOMDesign();
    ;
 
    bomDesign.itemExplode( 'D0001', 0 );
}
6.I have temporarily changed the TableType to regular so I can see the results.
TmpBOMExplodeTable

Viewing all articles
Browse latest Browse all 181

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>