Wednesday, July 24, 2013

Inventory master data migration AX 2012

Sometimes using DMF is quite hard if you don't know how to configure and use it, below is the sample code how to do the inventory master from code (not product master).
Actually we can always use standard AX 2012 code to release the product master but we need to configure it again once it's become the inventory master; so below code can help to release the inventory master with pre-configured data that you can define from a CSV file.
We will use help from AIF class InventItemService.
    

static void AIFInventMaster(Args _args)
{
    #AifDimensionDataContainerTypes
    #define.ItemId(1)
    #define.Product(1)
    #define.ItemName(2)
    #define.InventUOM(8)
    #define.SalesUOM(99)
    #define.PurchUOM(99)
    #define.ItemGroup(6)
    #define.ItemModelGroup(14)
    #define.StorageDimensionGroup(12)
    #define.TrackingDimensionGroup(13)
    #define.InventSiteId(0)
    #define.FinDimDepartment(15)
    #define.FinDimCostCenter(16)
    #define.FinDimPurpose(17)

//
#File
    IO                      io;
    container               dataLine;
    container               data;
    int                     k;
    FileIOPermission        fileIOPermission;


  
    str fileName = @'D:\ImportData\ProductItemMaster.csv';
    container conData, row;
    int i,j, columns;
    HcmPersonSkill hcmPersonSkill;
    TableId tableId;

    Struct              struct = new Struct();
    container           conDefaultDim;
    DimensionDefault    DimensionDefault;

    InventItemService                       inventService;
    InventItem                              inventItem;
    InventItem_InventTable                  inventTable;
    InventItem_Invent                       inventTableModuleInvent;
    InventItem_Sales                        inventTableModuleSales;
    InventItem_Purch                        inventTableModulePurch;

    InventItem_InventDimInventSetup         inventDimInventSetup;
    InventItem_InventDimPurchSetup          inventDimPurchSetup;
    InventItem_InventDimSalesSetup          inventDimSalesSetup;
    InventItem_DefaultInventDimInventSetup  inventDefaultInventdimInventSetup;
    InventItem_DefaultInventDimSalesSetup   inventDefaultInventDimSalesSetup;
    InventItem_DefaultInventDimPurchSetup   inventDefaultInventDimPurchSetup;

    InventItem_InventItemGroupItem          inventItemGroupItem;
    InventItem_InventItemInventSetup        inventItemInventSetup;
    InventItem_InventItemPurchSetup         inventItemPurchSetup;
    InventItem_InventItemSalesSetup         inventItemSalesSetup;
    InventItem_InventModelGroupItem         inventModelGroupItem;
    InventItem_StorageDimensionGroup        inventStorageDimensionGroup;
    InventItem_TrackingDimensionGroup       inventTrackingDimensionGroup;
    //inventitem_



    EcoResEcoResProduct                      ecoResProd;
    EcoResProductType                        ecoResProductType;
    boolean                                  isMaster = false;

    EcoResProductName                       _ecmProductName;
    EcoResProductSubtype                    prodSubType;

    ItemId                                  itemId;
    InventDim                               inventDim;

    AifDimensionAttributeValueSet               DefaultDimSet;
    AifDimensionAttributeValue                  dimensionAttributeValue;
    AfStronglyTypedDataContainerList            dimensionAttributeValues;

    void createInventMaster()
    {
        dimensionAttributeValues = null;
        if (conPeek(row,#FinDimDepartment) || conPeek(row,#FinDimCostCenter) || conPeek(row,#FinDimPurpose))
        {
            dimensionAttributeValues = new AfStronglyTypedDataContainerList(#AifDimensionAttributeValue);

            if (conPeek(row,#FinDimDepartment))
            {
                dimensionAttributeValue = dimensionAttributeValues.addNew();
                dimensionAttributeValue.parmName("Department");
                dimensionAttributeValue.parmValue(conPeek(row,#FinDimDepartment));
            }

            if (conPeek(row,#FinDimCostCenter))
            {
                dimensionAttributeValue = dimensionAttributeValues.addNew();
                dimensionAttributeValue.parmName("CostCenter");
                dimensionAttributeValue.parmValue(conPeek(row,#FinDimCostCenter));
            }

            if (conPeek(row,#FinDimPurpose))
            {
                dimensionAttributeValue = dimensionAttributeValues.addNew();
                dimensionAttributeValue.parmName("Purpose");
                dimensionAttributeValue.parmValue(conPeek(row,#FinDimPurpose));
            }
        }



        //init value
        inventDim.InventSiteId = "SSH";
        inventDim = InventDim::findOrCreate(inventDim);

        try
        {
            inventService = InventItemService::construct();
            inventItem = new InventItem();

            inventTable = new InventItem_InventTable();
            inventTable.parmItemId(conPeek(row,#ItemId));
            inventTable.parmProduct(conPeek(row,#Product));
            if (dimensionAttributeValues)
            {
                inventTable.createDefaultDimension().parmValues(dimensionAttributeValues);
            }

            inventStorageDimensionGroup = inventTable.createStorageDimensionGroup().addNew();
            inventStorageDimensionGroup.parmItemDataAreaId("SHPL");
            inventStorageDimensionGroup.parmItemId(conPeek(row,#ItemId));
            inventStorageDimensionGroup.parmStorageDimensionGroup(conPeek(row,#StorageDimensionGroup));

            inventTrackingDimensionGroup = inventTable.createTrackingDimensionGroup().addNew();
            inventTrackingDimensionGroup.parmItemDataAreaId("SHPL");
            inventTrackingDimensionGroup.parmItemId(conPeek(row,#ItemId));
            inventTrackingDimensionGroup.parmTrackingDimensionGroup(conPeek(row,#TrackingDimensionGroup));

            inventItemGroupItem = inventTable.createInventItemGroupItem().addNew();
            inventItemGroupItem.parmItemGroupId(conPeek(row,#ItemGroup));
            inventItemGroupItem.parmItemId(conPeek(row,#ItemId));
            inventItemGroupItem.parmItemDataAreaId("SHPL");
            inventItemGroupItem.parmItemGroupDataAreaId("SHPL");

            inventTableModuleInvent = inventTable.createInvent().addNew();
            inventTableModuleInvent.parmItemId(conPeek(row,#ItemId));
            inventTableModuleInvent.parmUnitId(conPeek(row,#InventUOM));

            inventTableModuleSales = inventTable.createSales().addNew();
            inventTableModuleSales.parmItemId(conPeek(row,#ItemId));
            //inventTableModuleSales.parmUnitId(conPeek(row,#InventUOM));

            inventTableModuleInvent = inventTable.createPurch().addNew();
            inventTableModuleInvent.parmItemId(conPeek(row,#ItemId));
            //inventTableModuleInvent.parmUnitId(conPeek(row,#InventUOM));

            inventModelGroupItem = inventTable.createInventModelGroupItem().addNew();
            inventModelGroupItem.parmItemDataAreaId("SHPL");
            inventModelGroupItem.parmItemId(conPeek(row,#ItemId));
            inventModelGroupItem.parmModelGroupDataAreaId("SHPL");
            inventModelGroupItem.parmModelGroupId(conPeek(row,#ItemModelGroup));

            inventItemInventSetup = inventTable.createInventItemInventSetup().addNew();
            inventItemInventSetup.parmItemId(conPeek(row,#itemId));
            //inventItemInventSetup.parmInventDimId(inventDim.inventDimId);
            //inventItemInventSetup.parmInventDimIdDefault(inventDim.inventDimId);
            inventDefaultInventdimInventSetup = inventItemInventSetup.createDefaultInventDimInventSetup().addNew();
            inventDefaultInventdimInventSetup.parmInventSiteId("SSH");
            inventDimInventSetup = inventItemInventSetup.createInventDimInventSetup().addNew();
            inventDimInventSetup.parmInventSiteId("");

            inventItemPurchSetup = inventTable.createInventItemPurchSetup().addNew();
            inventItemPurchSetup.parmItemId(conPeek(row,#itemId));
            //inventItemPurchSetup.parmInventDimId(inventDim.inventDimId);
            //inventItemPurchSetup.parmInventDimIdDefault(inventDim.inventDimId);
            inventDefaultInventdimPurchSetup = inventItemPurchSetup.createDefaultInventDimPurchSetup().addNew();
            inventDefaultInventdimPurchSetup.parmInventSiteId("SSH");
            inventDimPurchSetup = inventItemPurchSetup.createInventDimPurchSetup().addNew();
            inventDimPurchSetup.parmInventSiteId("");

            inventItemSalesSetup = inventTable.createInventItemSalesSetup().addNew();
            inventItemSalesSetup.parmItemId(conPeek(row,#itemId));
            //inventItemSalesSetup.parmInventDimId(inventDim.inventDimId);
            //inventItemSalesSetup.parmInventDimIdDefault(inventDim.inventDimId);
            inventDefaultInventdimSalesSetup = inventItemSalesSetup.createDefaultInventDimSalesSetup().addNew();
            inventDefaultInventdimSalesSetup.parmInventSiteId("SSH");
            inventDimSalesSetup = inventItemSalesSetup.createInventDimSalesSetup().addNew();
            inventDimSalesSetup.parmInventSiteId("");

            inventItem.createInventTable().add(inventTable);
            inventService.create(inventItem);
        }
        catch(Exception::Error)
        {
            throw Exception::Error;
        }
        catch(Exception::Deadlock)
        {
            retry;
        }
        catch(Exception::UpdateConflict)
        {
            if(appl.ttsLevel() == 0)
            {
                if(xSession::currentRetryCount() >= 4)
                {
                    throw Exception::UpdateConflictNotRecovered;
                }
                else
                {
                    retry;
                }
            }
            else
            {
                throw Exception::UpdateConflict;
            }
        }
    }

    ;
    
    fileIOPermission = new FileIOPermission(fileName, #IO_Read);
    fileIOPermission.assert();

io = new CommaTextIo(fileName, #IO_Read);
io.inFieldDelimiter(",");

if (! io || io.status() != IO_Status::Ok)
{
throw error("@SYS19358");
}

//filled in the data to one container for other purpose
//can modify this to direct use it
while (io.status() == IO_Status::Ok)
{
k++;
dataLine = io.read();
if (dataLine)
{
conData = conIns(conData,k,dataLine);
}
}
    
    for (i = 2; i <= conLen(conData); i++)
    {        
        columns = conLen(conPeek(conData, i));       
        row = conpeek(conData, i);

        try
        {
            ttsBegin;

            createInventMaster();

            ttsCommit;
        }
        catch
        {
            info(strFmt('Error importing line %1, inventtable: %2', i, conPeek(row, 1)));
        }
    }
io.finalize();

io = null;
    info("Process completed.");
}


1 comment: