Dynamics AX 2012 Data Import using X++


Microsoft Dynamics AX 2012 Xpp –
Sales Orders Import
 
Purpose: The purpose of this document is to illustrate how to write a required minimum X++ code in Microsoft Dynamics AX 2012 in order to import Sales orders.
 
Challenge: Data model changes in Microsoft Dynamics AX 2012 related to high normalization and introduction of surrogate keys made some imports more complex. However the structure of tables comprising Sales order header/lines didn't change. Please note that after you import Sales orders you may need to perform full/partial Physical and/or Financial update if required (for example, for Purchase orders already in execution).
 
Solution: Appropriate tables buffers (SalesTable, SalesLine) will be used when writing X++ code in Microsoft Dynamics AX 2012 in order to import Sales orders. Alternatively AxBC classes may be used instead of table buffers.
 
Data Model:
 
Table Name
Table Description
SalesTable
The SalesTable table contains all sales order headers regardless of whether they have been posted.
SalesLine
The SalesLine table contains all sales order lines regardless of whether they have been posted.
InventDim
The InventDim table contains values for inventory dimensions.
 
Data Model Diagram:
<![if !vml]><![endif]>
 
Development:
 
ttsBegin: Use ttsBegin to start a transaction.
 
clear: The clear method clears the contents of the record.
 
initValue: The initValue method initializes the fields of the record.
initFrom*: The initFrom* methods usually populate the fields of the child record based on the fields on the parent record. Example is initFromSalesTable method on SalesLine table.
 
validateWrite: The validateWrite method checks whether the record can be written.
write: The write method writes the record to the database.
 
insert: The insert method inserts the record into the database.
doInsert: The doInsert method inserts the record into the database. Calling doInsert ensures that any X++ code written in the insert method of the record is not executed. Calling insert always executes the X++ code written in theinsert method of the record.
 
ttsCommit: Use ttsCommit to commit a transaction.
 
Source code:
static void SalesOrdersXppImport(Args _args)
{
    #define.Customer("US-001")
    #define.DeliveryDate("1/1/2014")
    #define.ItemId("M0001")
    #define.Qty(10)
    #define.Unit("ea")
 
    SalesTable      salesTable;
    SalesLine       salesLine;
    InventDim       inventDim;
 
    try
    {
        ttsbegin;
 
        //Order header
        salesTable.clear();
        salesTable.initValue(SalesType::Sales);
        salesTable.SalesId = NumberSeq::newGetNum(SalesParameters::numRefSalesId()).num();
        salesTable.DeliveryDate = str2Date(#DeliveryDate, 213);
        salesTable.CustAccount = #Customer;
        salesTable.initFromCustTable();       
 
        if (salesTable.validateWrite())
        {
            salesTable.insert();
 
            //Order line
            inventDim.clear();
            inventDim.InventSiteId = "1";
            inventDim.InventLocationId = "12";
 
            salesLine.clear();
            salesLine.initValue(salesTable.SalesType);
            salesLine.initFromSalesTable(salesTable);
            salesLine.ItemId = #ItemId;
            salesLine.initFromInventTable(InventTable::find(#ItemId));
 
            salesLine.InventDimId = InventDim::findOrCreate(inventDim).inventDimId;
            salesLine.SalesQty = #Qty;
            salesLine.RemainSalesPhysical = salesLine.SalesQty;
            salesLine.SalesUnit = #Unit;
            salesLine.QtyOrdered = salesLine.calcQtyOrdered();
            salesLine.RemainInventPhysical = salesLine.QtyOrdered;
 
            salesLine.setPriceDisc(InventDim::find(salesLine.InventDimId));
 
            if (salesLine.validateWrite())
            {
                salesLine.insert();
            }
            else
                throw error("Order line");
        }
        else
            throw error("Order header");
 
        ttscommit;
    }
    catch
    {
        error("Error!");
        return;
    }
 
    info("Done!");
}
 

Comments

Popular posts from this blog

Dynamics Ax 2012 Interview Questions:

Oops concept in AX