Search This Blog

Friday, July 6, 2012

AX2012 Import Items from CSV-part 2.


This was the continuation of my earlier post for importing items from CSV.
Please refer post before going into this code.


http://krishhdax.blogspot.dk/2012/05/ax2012-import-items-from-csv-file.html

MACROS that I was using for this code.

 #Define.InventTable_ItemId_ColumnNum(1)
    #Define.InventTable_PrimaryVendorId_ColumnNum(2)
    #Define.InventTable_NetWeight_ColumnNum(3)
    #Define.InventTable_UnitVolume_ColumnNum(4)
    #Define.InventTable_BOMUnitId_ColumnNum(5)
    #Define.InventTable_AltItemId_ColumnNum(6)
    #Define.InventTable_Intracode_ColumnNum(7)
    #Define.InventTable_ABCRevenue_ColumnNum(8)
    #Define.InventTable_NameAlias_ColumnNum(9)
    #Define.InventTable_ProdGroupId_ColumnNum(10)
    #Define.InventTable_SerialNumGroupId_ColumnNum(11)
    #Define.InventTable_ItemBuyerGroupId_ColumnNum(12)
    #Define.InventTable_DefaultDimension_ColumnNum(13)
    #Define.InventTable_PmfProductType_ColumnNum(14)
 
    #Define.InventItemLocation_ItemId_ColumnNum(24)
    #Define.InventItemGroupItem_ItemId_ColumnNum(25)
    #Define.InventItemGroupItem_ItemGroupId_ColumnNum(26)
    #Define.InventModelGroupItem_ModelGroupId_ColumnNum(27)
    #Define.InventModelGroupItem_ItemId_ColumnNum(28)
    #Define.EcoResProductTranslation_Description_ColumnNum(29)
    #Define.EcoResProductTranslation_Name_ColumnNum(30)
    #Define.InventItemPurchSetup_ItemId_ColumnNum(31)
    #Define.InventItemPurchSetup_MultipleQty_ColumnNum(32)
    #Define.InventItemPurchSetup_LowestQty_ColumnNum(33)
    #Define.InventItemPurchSetup_HighestQty_ColumnNum(34)
    #Define.InventItemPurchSetup_LeadTime_ColumnNum(35)
    #Define.InventItemPurchSetup_CalendarDays_ColumnNum(36)
    #Define.InventItemPurchSetup_Stopped_ColumnNum(37)
    #Define.InventItemInventSetup_ItemId_ColumnNum(38)
    #Define.InventItemInventSetup_MultipleQty_ColumnNum(39)
    #Define.InventItemInventSetup_LowestQty_ColumnNum(40)
    #Define.InventItemInventSetup_HighestQty_ColumnNum(41)
    #Define.InventItemInventSetup_StandardQty_ColumnNum(42)
    #Define.InventItemInventSetup_Stopped_ColumnNum(43)
    #Define.InventItemSalesSetup_Stopped_ColumnNum(44)
    #Define.InventItemSetupSupplyType_DefaultOrderType_ColumnNum(45)
    #Define.InventItemSetupSupplyType_ItemId_ColumnNum(46)
    #Define.InventTableModulePurch_ItemId_ColumnNum(47)
    #Define.InventTableModulePurch_UnitId_ColumnNum(48)
    #Define.InventTableModuleSales_ItemId_ColumnNum(49)
    #Define.InventTableModuleSales_UnitId_ColumnNum(50)
    #Define.InventTableModuleInvent_ItemId_ColumnNum(51)
    #Define.InventTableModuleInvent_UnitId_ColumnNum(52)
    #Define.EcoResStorageDimensionGroupItem_ItemId_ColumnNum(53)
    #Define.EcoResStorageDimensionGroupItem_StorageDimensionGroup_ColumnNum(54)
    #Define.EcoResTrackingDimensionGroupItem_ItemId_ColumnNum(55)
    #Define.EcoResTrackingDimensionGroupItem_TrackingDimensionGroup_ColumnNum(56)
    #Define.ReqItemTable_ItemId_ColumnNum(57)
    #Define.ReqItemTable_MaxInventOnHand_ColumnNum(58)
    #Define.ReqItemTable_MinInventOnHand_ColumnNum(59)
    #Define.ReqItemTable_ReqGroupId_ColumnNum(60)
    #Define.ReqItemTable_LeadTimeProduction_ColumnNum(61)


// this method is used to find  and create the dimension values if not not found.

RecId findCreateDimension(str _project, str _department, str _sapProductCode = "", str _item = "", str _vendor="", str _customer="",str _orginizationvalue)
{
    RecId                               ret;

    DimensionAttributeValueSetStorage   dimStorage;
    DimensionAttributeValue             dimensionAttributeValue;
    DimensionAttribute                  dimensionAttribute;
    ;

    if (_project || _department || _sapProductCode || _item || _vendor || _customer)
    {
        dimStorage              = new DimensionAttributeValueSetStorage();

        if (_project)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("project");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _project);
            dimStorage.addItem(dimensionAttributeValue);
        }

        if (_department)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("Department");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _department);
            dimStorage.addItem(dimensionAttributeValue);

           
            if( _orginizationvalue )
            {
                dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("Organization");
                dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute,  _orginizationvalue );
                dimStorage.addItem(dimensionAttributeValue);
            }
        }

        if (_sapProductCode)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("SAPProductCode");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _sapProductCode);
            dimStorage.addItem(dimensionAttributeValue);
        }

        if (_item)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("Item");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _item);
            dimStorage.addItem(dimensionAttributeValue);
        }

        if (_vendor)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("Vendor");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _vendor);
            dimStorage.addItem(dimensionAttributeValue);
        }

        if (_customer)
        {
            dimensionAttribute      = AxdDimensionUtil::validateFinancialDimension("Customer");
            dimensionAttributeValue = AxdDimensionUtil::validateFinancialDimensionValue(dimensionAttribute, _customer);
            dimStorage.addItem(dimensionAttributeValue);
        }

        ret = dimStorage.save();
    }
    return ret;
}


protected void createInventTable(container _conLine)
{
    ItemId  itemId;
    InventTable inventTable;
    InventItemGroupItem inventItemGroupItem;
    InventDimGroupSetup inventDimGroupSetup;
    InventDim           inventDim;
    inventDim           inventDim_InventSiteId, inventDim_InventLocationId;
    #Define.DefaultInventSiteId("Melb")
    #Define.DefaultInventLocationId("Kilda")
    ;


    itemId  =   this.getLineValue(_conLine,#InventTable_ItemId_ColumnNum);
    inventTable= InventTable::find(itemId);
    if(!inventTable)
    {
        ecoResDistictProduct.clear();
        ecoResDistictProduct.initValue();
        ecoResDistictProduct.DisplayProductNumber=itemId;
        ecoResDistictProduct.ProductType=ecoResProductType::Item;
        ecoResDistictProduct.SearchName=this.getLineValue(_conLine,#InventTable_NameAlias_ColumnNum);

        ecoResDistictProduct.insert();

        //Product Translation
        axProductTranslation=AxEcoResProductTranslation::construct();
        axProductTranslation.parmDescription(this.getLineValue(_conLine,#EcoResProductTranslation_Description_ColumnNum));
        axProductTranslation.parmName(this.getLineValue(_conLine,#EcoResProductTranslation_Name_ColumnNum));
        axProductTranslation.parmLanguageId(CompanyInfo::languageId());
        axProductTranslation.parmProduct(ecoResDistictProduct.RecId);

        axProductTranslation.save();


        axInventTable=axInventTable::construct();
        axInventTable.parmItemId(this.getLineValue(_conLine,#InventTable_ItemId_ColumnNum));
        axInventTable.parmItemType(ItemType::Item);

        axInventTable.parmPrimaryVendorId(this.getLineValue(_conLine,#InventTable_PrimaryVendorId_ColumnNum));
        axInventTable.parmNetWeight(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventTable_NetWeight_ColumnNum)));
        axInventTable.parmProduct(ecoResDistictProduct.RecId);

        axInventTable.parmUnitVolume(dtSource.convertStr2Num(this.getLineValue(_conLine,#InventTable_UnitVolume_ColumnNum)));

        axInventTable.parmUseAltItemId(ItemNumAlternative::Never);
        axInventTable.parmAltItemId(this.getLineValue(_conLine,#InventTable_AltItemId_ColumnNum));
        axInventTable.parmIntracode(this.getLineValue(_conLine,#InventTable_Intracode_ColumnNum));
        axInventTable.parmABCRevenue(str2enum(ABC, this.getLineValue(_conLine,#InventTable_ABCRevenue_ColumnNum)));
        axInventTable.parmNameAlias(this.getLineValue(_conLine,#InventTable_NameAlias_ColumnNum));
       
        axInventTable.parmBOMUnitId(this.getLineValue(_conLine,#InventTable_BOMUnitId_ColumnNum));
        axInventTable.parmProdGroupId(this.getLineValue(_conLine,#InventTable_ProdGroupId_ColumnNum));
        axInventTable.parmSerialNumGroupId(this.getLineValue(_conLine,#InventTable_SerialNumGroupId_ColumnNum));
        axInventTable.parmItemBuyerGroupId(this.getLineValue(_conLine,#InventTable_ItemBuyerGroupId_ColumnNum));
        axInventTable.parmPmfProductType(this.getLineValue(_conLine,#InventTable_PmfProductType_ColumnNum) == "Yes" ? PmfProductType::BOM : PmfProductType::None);
       

        //InventTableModulePurch
        axInventTableModulePurch = AxInventTableModule::construct();
        axInventTableModulePurch.axInventTable(axInventTable);
        axInventTableModulePurch.parmItemId(this.getLineValue(_conLine, #InventTableModulePurch_ItemId_ColumnNum));
        axInventTableModulePurch.parmModuleType(ModuleInventPurchSales::Purch);
        axInventTableModulePurch.parmUnitId(this.getLineValue(_conLine, #InventTableModulePurch_UnitId_ColumnNum));
        axInventTable.axInventTableModule_Purch(axInventTableModulePurch);

        //InventTableModuleSales
        axInventTableModuleSales = AxInventTableModule::construct();
        axInventTableModuleSales.axInventTable(axInventTable);
        axInventTableModuleSales.parmItemId(this.getLineValue(_conLine, #InventTableModuleSales_ItemId_ColumnNum));
        axInventTableModuleSales.parmModuleType(ModuleInventPurchSales::Sales);
        axInventTableModuleSales.parmUnitId(this.getLineValue(_conLine, #InventTableModuleSales_UnitId_ColumnNum));
        axInventTable.axInventTableModule_Sales(axInventTableModuleSales);

        //InventTableModuleInvent
        axInventTableModuleInvent = AxInventTableModule::construct();
        axInventTableModuleInvent.axInventTable(axInventTable);
        axInventTableModuleInvent.parmItemId(this.getLineValue(_conLine, #InventTableModuleInvent_ItemId_ColumnNum));
        axInventTableModuleInvent.parmModuleType(ModuleInventPurchSales::Invent);
        axInventTableModuleInvent.parmUnitId(this.getLineValue(_conLine, #InventTableModuleInvent_UnitId_ColumnNum));
        axInventTable.axInventTableModule_Invent(axInventTableModuleInvent);

        //EcoResTrackingDimensionGroupItem
        axEcoResTrackingDimensionGroupItem = AxEcoResTrackingDimensionGroupItem::construct();
        axEcoResTrackingDimensionGroupItem.axInventTable(axInventTable);
        axEcoResTrackingDimensionGroupItem.parmItemDataAreaId(axInventTable.inventTable().dataAreaId);
        axEcoResTrackingDimensionGroupItem.parmItemId(itemId);
        axEcoResTrackingDimensionGroupItem.parmTrackingDimensionGroup(EcoResTrackingDimensionGroup::findByDimensionGroupName(this.getLineValue(_conLine, #EcoResTrackingDimensionGroupItem_TrackingDimensionGroup_ColumnNum)).RecId);
        axInventTable.axEcoResTrackingDimensionGroupItem(axEcoResTrackingDimensionGroupItem);

        //EcoResTrackingDimensionGroupItem
        axEcoResStorageDimensionGroupItem = AxEcoResStorageDimensionGroupItem::construct();
        axEcoResStorageDimensionGroupItem.axInventTable(axInventTable);
        axEcoResStorageDimensionGroupItem.parmItemDataAreaId(axInventTable.inventTable().dataAreaId);
        axEcoResStorageDimensionGroupItem.parmItemId(itemId);
        axEcoResStorageDimensionGroupItem.parmStorageDimensionGroup(EcoResStorageDimensionGroup::findByDimensionGroupName(this.getLineValue(_conLine, #EcoResStorageDimensionGroupItem_StorageDimensionGroup_ColumnNum)).RecId);
        axInventTable.axEcoResStorageDimensionGroupItem(axEcoResStorageDimensionGroupItem);

        axInventTable.save();

 

        //EcoResTrackingDimensionGroupProduct
        axEcoResTrackingDimensionGroupProduct = AxEcoResTrackingDimensionGroupProduct::construct();
        axEcoResTrackingDimensionGroupProduct.parmProduct(ecoResProduct.RecId);
        axEcoResTrackingDimensionGroupProduct.parmTrackingDimensionGroup(EcoResTrackingDimensionGroup::findByDimensionGroupName(this.getLineValue(_conLine, #EcoResTrackingDimensionGroupItem_TrackingDimensionGroup_ColumnNum)).RecId);
        axEcoResTrackingDimensionGroupProduct.save();

        //EcoResStorageDimensionGroupProduct
        axEcoResStorageDimensionGroupProduct = AxEcoResStorageDimensionGroupProduct::construct();
        axEcoResStorageDimensionGroupProduct.parmProduct(ecoResProduct.RecId);
        axEcoResStorageDimensionGroupProduct.parmStorageDimensionGroup(EcoResStorageDimensionGroup::findByDimensionGroupName(this.getLineValue(_conLine, #EcoResStorageDimensionGroupItem_StorageDimensionGroup_ColumnNum)).RecId);
        axEcoResStorageDimensionGroupProduct.save();

// creating the Dimension values
        axInventTable.parmDefaultDimension(
            findCreateDimension(""
                , ""
                , this.getLineValue(_conLine, #InventTable_DefaultDimension_ColumnNum)
                , this.getLineValue(_conLine, #InventTable_ItemId_ColumnNum)));

        axInventTable.save();

        inventDim_InventSiteId.clear();
        inventDim_InventSiteId.InventSiteId = #DefaultInventSiteId;
        inventDim_InventSiteId = InventDim::findOrCreate(inventDim_InventSiteId);

        inventDim_InventLocationId.clear();
        inventDim_InventLocationId.InventLocationId = #DefaultInventlocationId;
        inventDim_InventLocationId = InventDim::findOrCreate(inventDim_InventLocationId);

        //InventItemInventSetup
        axInventItemInventSetup = AxInventItemInventSetup::newInventItemInventSetup(InventItemInventSetup::findDefault(itemId, true));
        axInventItemInventSetup.axInventTable(axInventTable);
        axInventItemInventSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
        axInventItemInventSetup.parmLowestQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemInventSetup_LowestQty_ColumnNum)));
        axInventItemInventSetup.parmHighestQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemInventSetup_HighestQty_ColumnNum)));
        axInventItemInventSetup.parmStandardQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemInventSetup_StandardQty_ColumnNum)));
        axInventItemInventSetup.parmMultipleQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemInventSetup_MultipleQty_ColumnNum)));
        axInventItemInventSetup.parmStopped(str2enum(noYes, this.getLineValue(_conLine, #InventItemInventSetup_Stopped_ColumnNum)));
        axInventItemInventSetup.save();

        axInventItemInventSetup = AxInventItemInventSetup::construct();
        axInventItemInventSetup.axInventTable(axInventTable);
        axInventItemInventSetup.parmItemId(itemId);
        axInventItemInventSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
        axInventItemInventSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
        axInventItemInventSetup.save();

        //InventPurchSetup
        axInventItemPurchSetup  = axInventItemPurchSetup::newInventItemPurchSetup(InventItemPurchSetup::findDefault(itemId, true));
        axInventItemPurchSetup.axInventTable(axInventTable);
        axInventItemPurchSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
        axInventItemPurchSetup.parmMultipleQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemPurchSetup_MultipleQty_ColumnNum)));
        axInventItemPurchSetup.parmLowestQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemPurchSetup_LowestQty_ColumnNum)));
        axInventItemPurchSetup.parmHighestQty(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemPurchSetup_HighestQty_ColumnNum)));
        axInventItemPurchSetup.parmLeadTime(dtSource.convertStr2Num(this.getLineValue(_conLine, #InventItemPurchSetup_LeadTime_ColumnNum)));
        axInventItemPurchSetup.parmCalendarDays(str2enum(CalendarDays, this.getLineValue(_conLine, #InventItemPurchSetup_CalendarDays_ColumnNum)));
        axInventItemPurchSetup.parmStopped(str2enum(noYes, this.getLineValue(_conLine, #InventItemPurchSetup_Stopped_ColumnNum)));
        axInventItemPurchSetup.save();

        axInventItemPurchSetup = AxInventItemPurchSetup::construct();
        axInventItemPurchSetup.axInventTable(axInventTable);
        axInventItemPurchSetup.parmItemId(itemId);
        axInventItemPurchSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
        axInventItemPurchSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
        axInventItemPurchSetup.save();

        //InventItemSalesSetup
        axInventItemSalesSetup  = AxInventItemSalesSetup::newInventItemSalesSetup(InventItemSalesSetup::findDefault(itemId, true));
        axInventItemSalesSetup.axInventTable(axInventTable);
        axInventItemSalesSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
        axInventItemSalesSetup.parmStopped(str2enum(noYes, this.getLineValue(_conLine, #InventItemSalesSetup_Stopped_ColumnNum)));
        axInventItemSalesSetup.save();

        axInventItemSalesSetup = AxInventItemSalesSetup::construct();
        axInventItemSalesSetup.axInventTable(axInventTable);
        axInventItemSalesSetup.parmItemId(itemId);
        axInventItemSalesSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
        axInventItemSalesSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
        axInventItemSalesSetup.save();

        //InventGroup Item
        axInventGroupItem=AxInventItemGroupItem::construct();
        axInventGroupItem.parmItemId(itemId);
        axInventGroupItem.parmItemGroupId(this.getLineValue(_conLine,#InventItemGroupItem_ItemGroupId_ColumnNum));
        axInventGroupItem.parmItemGroupDataAreaId(curext());
        axInventGroupItem.parmItemDataAreaId(curext());
        axInventGroupItem.save();

        //InventModelGroupItem
        axInventModelGroupItem=AxInventModelGroupItem::construct();
        axInventModelGroupItem.parmItemId(itemId);
        axInventModelGroupItem.parmModelGroupId(this.getLineValue(_conLine,#InventModelGroupItem_ModelGroupId_ColumnNum));
        axInventModelGroupItem.parmModelGroupDataAreaId(curext());
        axInventModelGroupItem.parmItemDataAreaId(curext());
        axInventModelGroupItem.save();


        //InventItemSetupSupplyType
        inventItemSetupSupplyType.clear();
        inventItemSetupSupplyType.initValue();
        inventItemSetupSupplyType.ItemId=itemId;
        inventItemSetupSupplyType.ItemDataAreaId=curext();
        inventItemSetupSupplyType.DefaultOrderType  = this.getLineValue(_conLine, #InventItemSetupSupplyType_DefaultOrderType_ColumnNum) == "Indkøbsvare" ? ReqPOType::Purch : ReqPOType::Production;
        inventItemSetupSupplyType.insert();


        //ReqItemTable
        inventDimGroupSetup = InventDimGroupSetup::newInventTable(axInventTable.inventTable());
        inventDim.initFromInventTable(axInventTable.inventTable());
        inventDim.clearNotCovPrDim(inventDimGroupSetup);
        inventDim.InventSiteId      = #DefaultInventSiteId;
        inventDim.InventLocationId  = #DefaultInventLocationId;

        reqItemTable.clear();
        reqItemTable.initValue();
        reqItemTable.initFromInventTable(axInventTable.inventTable());
        reqItemTable.ItemId             = this.getLineValue(_conLine, #ReqItemTable_ItemId_ColumnNum);
        reqItemTable.MaxInventOnhand    = dtSource.convertStr2Num(this.getLineValue(_conLine, #ReqItemTable_MaxInventOnHand_ColumnNum));
        reqItemTable.MinInventOnhand    = dtSource.convertStr2Num(this.getLineValue(_conLine, #ReqItemTable_MinInventOnHand_ColumnNum));
        reqItemTable.ReqGroupId         = this.getLineValue(_conLine, #ReqItemTable_ReqGroupId_ColumnNum);
        reqItemTable.CalendarDaysProduction = NoYes::Yes;
        reqItemTable.LeadTimeProduction     = dtSource.convertStr2Num(this.getLineValue(_conLine, #ReqItemTable_LeadTimeProduction_ColumnNum));
        reqItemTable.LeadTimeProductionActive = reqItemTable.LeadTimeProduction ? NoYes::Yes : NoYes::No;
        reqItemTable.CovInventDimId         = InventDim::findOrCreate(inventDim).InventDimId;
        reqItemTable.insert();
     
    }

}

1 comment:

  1. Hi
    I need to import the unit, price, storage dimension and tracking dimension from a csv file to the items already that exist in the inventtable of ax 2012 is that possible through coding

    ReplyDelete

Thanks for visiting my blog,
I will reply for your comment within 48 hours.

Thanks,
krishna.