Wednesday, January 2, 2013

Dynamics AX 2012: Import Inventory Journal Lines through code


01-02-2012

Import Inventory Journal Lines Through code in Dynamics AX 2012

Problem: My task was to import the journal lines in dynamics AX 2012 through code.

Solved:     There are two ways to import the code office add-ins and X++ code. Here I am going to explain importing journal lines through code. Because there are lot of blogs explaining the office –add in method. This code was given by my colleague Mahendran.

In below you can find the code used to import the journal lines. Here I missed out something; I hope you people can manage those things. Like,
·         X++ code to read data from excel
·         Proper class declarations and variable declarations
I am just going to give the heart of the solution. If you can’t make it up to this please feel free to mail me at hapkarm@outlook.com

    //Getting the essential values from Excel
    itemId                               = cells.item(row, 1).value().bStr();
    inventSiteId                       = cells.item(row, 2).value().bStr();
    inventLocationId               = cells.item(row, 3).value().bStr();
    inventBatchId                    = cells.item(row, 4).value().bStr();
    qty                                    = cells.item(row, 5).value().double();
    costPrice                           = cells.item(row, 6).value().double();
    mainAcc                            = cells.item(row, 7).value().bStr();
    department                        = cells.item(row, 8).value().bStr();
   
    locInventDim.inventBatchId                   = inventBatchId;
    locInventDim.InventSiteId                       = inventSiteId;
    locInventDim.InventLocationId             = inventLocationId;
    journalTrans.clear();
    journalTrans.initFromInventJournalTable(journalTable);
    journalTrans.TransDate                             = systemDateGet();
    journalTrans.ItemId                                    = itemId;

    //Finding out the invent dim id from given value
    journalTrans.InventDimId                        = InventDim::findOrCreate(locInventDim).inventDimId;
    journalTrans.PriceUnit                               = 1;
    journalTrans.Qty                                          = qty;
    journalTrans.CostPrice                              = costPrice;
    journalTrans.CostAmount                        = journalTrans.Qty * journalTrans.CostPrice;
    //Except these below two lines above things are same as like in dynamics AX 2009
    //If you want to insert the dimension value with account
    //In below code I have no dimension with main account so I just put 0, Instead I have two        
    //dimension
    //write a code like “[“MainAccount”, mainAcc, 2, Dinemsion1, dimVal1, Dimension2, dimVal2)
    offsetEntryPattern                                      = ["MainAccount",mainAcc,0];
    journalTrans.LedgerDimension              = AxdDimensionUtil::getLedgerAccountId(offsetEntryPattern);
    //Default dimension Value either you can insert the dimension value here.
    defaultDim                                                     = [1,"Department",department];
    journalTrans.DefaultDimension             =        AxdDimensionUtil::getDimensionAttributeValueSetId(defaultDim);
    journalTrans.insert();

If you need any assistance from above code feel free to put a mail on hapkarm@outlook.com