Interview Questions

Monday, September 21, 2015

Creating and starting a production order using X++ on Dynamics AX 2012

01
static void _CreateProductionOrder(Args _args)
02
{
03
    ProdQty         qty     = 100;
04
    ItemId          item    = 'youritemid';
05
 
06
    ProdTable       prodtable;
07
    InventTable     inventTable;
08
    InventDim       inventDim;
09
    ;
10
 
11
    // Initialize InventTable using the item
12
    inventTable = inventTable::find(youritemid);
13
 
14
    // Initialize the base values using the previous initialized inventtable.
15
    prodtable.initValue();
16
    prodtable.initFromInventTable(inventTable);
17
 
18
    prodtable.ItemId                = inventTable.ItemId;
19
    prodtable.DlvDate               = today();
20
    prodtable.QtySched              = qty;
21
    prodtable.RemainInventPhysical  = qty;
22
 
23
    // Initialize InventDim, using again the inventtable (Obrigatory)
24
    inventDim.initFromInventTable(inventTable);
25
 
26
    // Set the active BOM and Route, this in order to create the correspondind BOM lines and coproducts. Also to asign resources
27
    prodtable.BOMId = BOMVersion::findActive(prodtable.ItemId,
28
                                             prodtable.BOMDate,
29
                                             prodtable.QtySched,
30
                                             inventDim).BOMId;
31
 
32
    prodtable.RouteId = RouteVersion::findActive(prodtable.ItemId,
33
                                                 prodtable.BOMDate,
34
                                                 prodtable.QtySched,
35
                                                 inventDim).RouteId;
36
 
37
    // Initialize the BOMVersion
38
    prodtable.initBOMVersion();
39
    // Initialize RouteVersion
40
    prodtable.initRouteVersion();
41
 
42
    //Use ProdTableType class to create the production order
43
    prodtable.type().insert();
44
 
45
 
46
}


The code is fairly simple, it starts with the inventTable, and from there it searches for the BOM version and the route for the production order using the prodtable and the InventDim, with all set we just have to use the type().insert() method so the required InventTrans transactions are also created.
Next let’s see how to start the production order, this is fairly simple too, we will use the following code:
 X++ |  copy code |? 
1
void startProdOrder(args _args)
2
{
3
 ProdTable _prodTable;
4
 
5
 _prodTable = ProdTable::find(YOURPRODUCTIONORDER);
6
 _prodTable.autoUpdate(ProdStatus::StartedUp);
7
}
So first we initialize the production order, next we call the autoUpdate method, and we set the status we want, just take note that even if you can send any enum value as a parameter it won’t work if the order is beyond started.
Hope you like it!

No comments:

Post a Comment