Import product master

Posted by

Importing a product master in Dynamics AX at the time of implementation is a nightmare for most of the consultants or for those who are new into the Dynamics AX/D365 journey. Over the years, with several implementations, migrations, upgrades, I have made a simple utility that will import almost the required fields that are relevant to item master(you can add or remove the fields from this as required). I have created a class with few methods in it, sharing the x++ code below. I am calling this class from a menu item.

Along with the item master, this code will help you to impor theitem barcode along with this.

Method 1 Class declaration

class Vv_ImportProductMaster
{
    #Define.blank("AllBlank")
    InventTable                                 inventTable;
    InventTableModule                           inventTableModule;
    EcoResProductMasterModelingPolicy           ecoResProductMasterModelingPolicy;
    EcoResProductIdentifier                     ecoResProductIdentifier,ecoResProductIdentifierCheck ;
    EcoResSize                                  ecoResSize;
    EcoResColor                                 ecoResColor;
    EcoResConfiguration                         ecoResConfiguration;
    EcoResStyle                                 ecoResStyle;
    boolean                                     itemExit,ecoresItemExit;
    InventDim                                   inventDim;
    InventDim                                   inventDimTOFind;
    InventDimCombination                        inventDimCombination;
    InventItemBarCode                           inventItemBarCode;
    RetailInventTable                           retailInventTable;
    ExtCodeValueTable                           extCodeValueTable;
    ExtCodeTable                                extCodeTable;
    EcoResProductDimensionGroup                 ecoResProductDimensionGroup;
    EcoResProductDimensionGroupProduct          ecoResProductDimensionGroupProduct;
    EcoResDistinctProduct                       ecoResDistinctProduct;
    EcoResProduct                               ecoResProduct;
    EcoResProductMaster                         ecoResProductMaster;
    EcoResProductMasterSize                     ecoResProductMasterSize;
    EcoResProductMasterColor                    ecoResProductMasterColor;
    EcoResProductMasterConfiguration            ecoResProductMasterConfig;
    EcoResProductMasterStyle                    ecoResProductMasterStyle;
    PriceDiscGroup                              priceDiscGroup;
    EcoResDistinctProductVariant                ecoResDistinctProductVariant;
    InventItemGroupItem                         inventItemGroupItem;
    str                                         segmentCode;
    ItemNetWeight                               itemNetWeight;
    int                                         row;
    VendAccount                                 defaultVendAccount;    
    EcoResCategoryHierarchyName                 CategoryHierarchyId;
    EcoResCategoryName                          ecoResCategoryId;
    EcoResVariantConfigurationTechnologyType    ecoResVariantConfigurationTechnologyType ;
    LanguageId                                  languageId;
    EcoResProductName                           ecoResProductName;
    EcoResProductTranslation                    ecoResProductTranslation, ecoResProductTranslation2;
    EcoResProductVariantColor                   ecoResProductVariantColor;
    EcoResProductVariantSize                    ecoResProductVariantSize;
    EcoResProductVariantConfiguration           ecoResProductVariantConfiguration;
    EcoResProductVariantStyle                   ecoResProductVariantStyle;
    EcoResProductType                           productType ;
    EcoResProductSubtype                        ecoResProductSubtype;
    str                                         productSubType,configuration,styles;
    EcoResProductSearchName                     ecoResProductSearchName;
    EcoResProductDisplayProductNumber           ecoResProductDisplayProductNumber;
    EcoResProductDisplayProductNumber           ecoResProductDisplayProductNumberVariant;
    EcoResStorageDimensionGroup                 ecoResStorageDimensionGroup;
    EcoResStorageDimensionGroupProduct          ecoResStorageDimensionGroupProduct;
    EcoResStorageDimensionGroupItem             ecoResStorageDimensionGroupItem;
    EcoResTrackingDimensionGroup                ecoResTrackingDimensionGroup;
    EcoResTrackingDimensionGroupProduct         ecoResTrackingDimensionGroupProduct;
    EcoResTrackingDimensionGroupItem            ecoResTrackingDimensionGroupItem;
    EcoResCategoryHierarchy                     ecoResCategoryHierarchy;
    EcoResCategory                              ecoResCategory;
    EcoResProductCategory                       ecoResProductCategory;
    EcoResProductMasterDimensionValue           ecoResProductMasterDimensionValue ;
    EcoResProductMasterDimValueTranslation      ecoResProductMasterDimValueTranslation;
    AxEcoResDistinctProduct                     distinctProduct ;
    AxInventTable                               axinventTable ;
    AxEcoResProductMasterColor                  axEcoResProductMasterColor;
    AxEcoResProductMasterConfiguration          axEcoResProductMasterConfiguration;
    AxEcoResProductMasterStyle                  axEcoResProductMasterStyle;
    AxEcoResProductMasterSize                   axEcoResProductMasterSize;
    AxEcoResDistinctProductVariant              axEcoResDistinctProductVariant ;
    AxInventDimCombination                      axInventDimCombination;
    AxEcoResProductMaster                       axEcoResProductMaster ;
    AxEcoResProductVariantColor                 axEcoResProductVariantColor ;
    AxEcoResProductVariantSize                  axEcoResProductVariantSize ;
    AxEcoResProductVariantConfiguration         axEcoResProductVariantConfiguration;
    AxEcoResProductVariantStyle                 axEcoResProductVariantStyle;
    AxEcoResProductDimensionGroupProduct        axEcoResProductDimensionGroupProduct ;
    InventModelGroupItem                        inventModelGroupItem;
    AxInventModelGroupItem                      axInventModelGroupItem ;
    AxEcoResProductTranslation                  axEcoResProductTranslation;
    AxEcoResProductTranslation                  axEcoResProductTranslation2 ;
    AxInventItemGroupItem                       axInventItemGroupItem ;
    AxEcoResStorageDimensionGroupItem           axEcoResStorageDimensionGroupItem ;
    AxEcoResProductMasterDimensionValue         axEcoResProductMasterDimensionValue ;
    AxEcoResProductMasterDimValueTranslation    axEcoResProductMasterDimValueTranslation;
    AxEcoResStorageDimensionGroupProduct        axEcoResStorageDimensionGroupProduct ;
    AxEcoResTrackingDimensionGroupItem          axEcoResTrackingDimensionGroupItem ;
    AxEcoResTrackingDimensionGroupProduct       axEcoResTrackingDimensionGroupProduct ;
    RetailSeasonTable                           retailSeasonTable;
    // Variables
    BarcodeSetupId                              barCodeSetupId;
    ItemBarCode                                 itemBarcode;
    ItemGroupId                                 itemGroupId;
    ItemID                                      itemId;
    ItemName                                    itemName, variantName, itemSearchName;
    InventModelGroupId                          modelGroupId;
    EcoResProductDimensionGroupName             dimGroupId;
    EcoResStorageDimensionGroupName             dimStorageId;
    EcoResTrackingDimensionGroupName            dimTrackingId;
    UnitIDBase                                  unitId;
    UnitIDBase                                  salesUnitId;
    UnitIDBase                                  purchUnitId;
    UnitIDBase                                  bomUnitId;
    RetailSeasonCode                            seasonCode;
    TransDate                                   launchDate;
    TaxItemGroup                                purchTaxItemGroup, salesTaxItemGroup;    
    ItemOrigCountryRegionId                     origCountry;
    ItemPackagingGroupId                        packingUnit;
    TaxPackagingQty                             packingQty;
    EcoResSizeName                              sizeCode;
    EcoResColorName                             colorCode;
    EcoResConfigurationName                     config;
    EcoResStyleName                             style;
    Name                                        colorName;
    str                                         vendorItemName, oldItemId,costGroupID,projectId;
    str 50                                      LineDiscCode;
    NoYes                                       zeroPricevalid, addinRequired, isVariant;
    PmfProductType                              pmfProductType;
    FnBItemAttributes                           fnBItemAttributes;
    OverDeliveryPct                             overDeliveryPct;
    boolean                                     isCostVarient,fnbProduceWORecipe;
    int                                         prodType;
    real                                        yieldPercent;
}

Method 2 Barcode creation

void createBarcode()
{
    InventItemBarcode       inventBarcode;
    if (itemBarCode)
        {
            select inventBarcode
                where inventBarcode.itemBarCode     ==  itemBarCode;

            if (!inventBarcode)
            {
                inventItemBarCode.initValue();
                inventItemBarCode.itemBarCode       = itemBarCode;
                inventItemBarCode.itemId            = itemId;
                inventItemBarCode.description       = variantName;
                inventItemBarCode.inventDimId       = inventDim.inventDimId;
                inventItemBarCode.barcodeSetupId    = barCodeSetupId;
                inventItemBarCode.UnitID            = salesUnitId;
                inventItemBarCode.RetailVariantId   = inventDimCombination.RetailVariantId;                
                inventItemBarCode.useForInput       = NoYes::Yes;
                inventItemBarCode.useForPrinting    = NoYes::Yes;
                inventItemBarCode.qty               = 1;
                inventItemBarCode.insert();
            }
            else
            {
                throw  error("Barcode %1 already exists!",itemBarCode);
            }
        }
}

Method 3 Data validation

boolean dataValidation()
{
    boolean ret = true;
    str lname;
    InventTable inventTableValidation;
    InventDimCombination   inventDimCombinationValidate;
    EcoResDistinctProductVariant ecoResDistinctProductVariantValidate;

    if (!itemId)
    {
        ret = checkFailed(strFmt("ItemId not provided in row now %1", row));
    }

    ecoResProductDisplayProductNumber           = itemId;
    ecoResProduct                               = EcoResProduct::findByDisplayProductNumber(ecoResProductDisplayProductNumber);
    ecoResProductDisplayProductNumberVariant    = strFmt(itemId + " : " + config + " : " + sizeCode + " : " + colorCode + " : " + style);
    productType     = prodType;
    inventTableValidation = InventTable::find(itemId);

    select * from ecoResDistinctProductVariant
        where ecoResDistinctProductVariant.ProductMaster        == ecoResProduct.RecId
        && ecoResDistinctProductVariant.ProductType             == productType
        &&  ecoResDistinctProductVariant.DisplayProductNumber   == ecoResProductDisplayProductNumberVariant;
        
    select * from inventDimCombinationValidate
            join ecoResDistinctProductVariantValidate
            where inventDimCombinationValidate.DistinctProductVariant  == ecoResDistinctProductVariantValidate.RecId
            && ecoResDistinctProductVariantValidate.DisplayProductNumber == ecoResProductDisplayProductNumberVariant
            && inventDimCombinationValidate.ItemId                       == inventTableValidation.ItemId;

    if (ret && ecoResProduct && ecoResDistinctProductVariant)
    {        
        info(strFmt("Import skipped : Item %1 already exist with same product dimension in row no %2", itemId, row)) ;
        ecoresItemExit = true;
    }
    else
    {
        ecoresItemExit = false;
    }
    if (ret && inventTableValidation && inventDimCombinationValidate)
    {
        ret = checkFailed(strFmt("Import skipped : Item %1 already exist in company %2 with same product dimension in row no %3", itemId,curext(), row)) ;
    }
    if (ret && dimGroupId)
    {
        ecoResProductDimensionGroup = EcoResProductDimensionGroup::findByDimensionGroupName(dimGroupId);
        if (!ecoResProductDimensionGroup)
        {
            ret = checkFailed(strFmt("Import skipped : Product dimension group %1 does not exist in row no %2", dimGroupId, row));

        }
    }
    if (ret && dimStorageId)
    {
        ecoResStorageDimensionGroup = EcoResStorageDimensionGroup::findByDimensionGroupName(dimStorageId);
        if (!ecoResStorageDimensionGroup)
        {
            ret = checkFailed(strFmt("Import skipped : Storage dimension group %1 does not exist in row no %2", dimStorageId, row));
        }
    }
    if (ret && dimTrackingId)
    {
        ecoResTrackingDimensionGroup = EcoResTrackingDimensionGroup::findByDimensionGroupName(dimTrackingId);
        if (!ecoResTrackingDimensionGroup)
        {
            ret = checkFailed(strFmt("Import skipped : Tracking dimension group %1 does not exist in row no %2", dimTrackingId, row));
        }
    }
    if (ret && itemGroupId && !InventItemGroup::find(itemGroupId))
    {
        ret = checkFailed(strFmt("Import skipped : Item group %1 does not exist in row no %2", itemGroupId, row));
    }
    if (ret && modelGroupId && !InventModelGroup::find(modelGroupId))
    {
        ret = checkFailed(strFmt("Import skipped : Model group Id %1 does not exist in row no %2", modelGroupId, row));
    }
    if (ret && purchTaxItemGroup && !TaxItemGroupHeading::find(purchTaxItemGroup))
    {
        ret = checkFailed(strFmt("Import skipped : Tax Item Group %1 does not exist in row no %2", purchTaxItemGroup, row));
    }
    if (ret && salesTaxItemGroup && !TaxItemGroupHeading::find(salesTaxItemGroup))
    {
        ret = checkFailed(strFmt("Import skipped : Tax Item Group %1 does not exist in row no %2", salesTaxItemGroup, row));
    }
    if (ret && barCodeSetupId && !(BarcodeSetup::find(barCodeSetupId)))
    {
        ret = checkFailed(strFmt("Import skipped : BarCodeSetUpId %1 does not exist in row no. %2 .", barCodeSetupId, row)) ;
    }
    if (ret && unitId && (!UnitOfMeasure::findBySymbol(unitId)))
    {
        ret = checkFailed(strFmt("Import skipped : Invent Unit Id %1 does not exist in row no %2", unitId, row));
    }
    if (ret && salesUnitId && (!UnitOfMeasure::findBySymbol(salesUnitId)))
    {
        ret = checkFailed(strFmt("Import skipped : Sales Unit Id %1 does not exist in row no %2", unitId, row));
    }
    if (ret && purchUnitId && (!UnitOfMeasure::findBySymbol(purchUnitId)))
    {
        ret = checkFailed(strFmt("Import skipped : Purch Unit Id %1 does not exist in row no %2", unitId, row));
    }
    if (ret && bomUnitId && (!UnitOfMeasure::findBySymbol(bomUnitId)))
    {
        ret = checkFailed(strFmt("Import skipped : BoM Unit Id %1 does not exist in row no %2", unitId, row));
    }
    if (ret && seasonCode && !RetailSeasonTable::find(seasonCode))
    {
        ret = checkFailed(strFmt("Import skipped : Season code %1 does not exist in row no %2", seasonCode, row));
    }
    return ret;
}

Method 4 Data validation

void importFromCSV()
{
    CommaTextIo                 file;
    container                   rec;
    Dialog                      dlg;
    DialogField                 dlgFld;
    ;

    dlg     = new Dialog("Import product master");
    dlgFld  = dlg.addField(ExtendedTypeStr(FilenameOpen));
    row     = 2;

    if (dlg.run())
    {
        file = new CommaTextIo(dlgFld.value(), 'r');
        file.inFieldDelimiter(',');
        rec = file.read();
        rec = file.read();
        ttsBegin;
        while (rec)
        {
            barCodeSetupId  = conPeek(rec, 1);
            itemBarcode     = conPeek(rec, 2);
            itemGroupId     = conPeek(rec, 3);
            itemId          = conPeek(rec, 4);
            itemName        = conPeek(rec, 5);
            modelGroupId    = conPeek(rec, 6);
            dimGroupId      = conPeek(rec, 7);
            dimStorageId    = conPeek(rec, 8);
            dimTrackingId   = conPeek(rec, 9);
            unitId          = conPeek(rec, 10);
            bomUnitId       = conPeek(rec, 11);
            purchUnitId     = conPeek(rec, 12);
            salesUnitId     = conPeek(rec, 13);
            seasonCode      = conPeek(rec, 14);
            launchDate      = str2Date(conPeek(rec, 15), 123);
            purchTaxItemGroup   = conPeek(rec, 16);
            salesTaxItemGroup   = conPeek(rec, 17);
            vendorItemName  = conPeek(rec, 18);
            packingUnit     = conPeek(rec, 19);
            packingQty      = str2num(conPeek(rec, 20));
            origCountry     = conPeek(rec, 21);
            sizeCode        = conPeek(rec, 22);
            colorCode       = conPeek(rec, 23);
            colorName       = conPeek(rec, 24);
            config          = conPeek(rec, 25);
            style          = conPeek(rec, 26);
            segmentCode     = conPeek(rec, 27);
            itemNetWeight   = str2num(conPeek(rec, 28));
            zeroPricevalid  = conPeek(rec, 29);
            pmfProductType  = str2enum(PmfProductType, conPeek(rec, 30));
            addinRequired   = conPeek(rec, 31);
            itemSearchName  = conPeek(rec, 32);
            oldItemId       = conPeek(rec, 33);
            overDeliveryPct = conPeek(rec, 34);
            defaultVendAccount  = conPeek(rec, 35);
            securityCenter      = conPeek(rec, 36);
            isCostVarient       = conPeek(rec, 37);
            prodType            = conPeek(rec, 38);
            yieldPercent        = conPeek(rec, 39);
            costGroupID         = conPeek(rec, 40);            
            projectId           = conPeek(rec, 41);
            productSubType      = conPeek(rec, 42);
            LineDiscCode        = conPeek(rec, 43);

            if (this.dataValidation())
            {
                inventTable  = InventTable::find(itemId);
                if(str2enum(ecoResProductSubtype,productSubType) == EcoResProductSubtype::Product )
                {
                    if (inventTable && inventTable.ItemType == ItemType::Item && inventTable.productSubtype() == EcoResProductSubtype::Product)
                    {
                        this.createBarcode();
                    }
                    dimGroupId = "";
                    this.importProductMaster();
                }
                if(str2enum(ecoResProductSubtype,productSubType) == EcoResProductSubtype::ProductMaster )
                {
                    this.importProductMaster();
                }
            }
            row ++;
            rec = file.read();
        }
        ttsCommit;
    }
}

Method 5 import product master

void importProductMaster()
{
    DimensionAttributeValueSetStorage           dimStorage;
    DimensionAttribute                          FinanceDimensionAttribute;
    DimensionAttributeValue                     newFinanceValue;
    ecoResVariantConfigurationTechnologyType    = EcoResVariantConfigurationTechnologyType::PredefinedVariants;    
    productType                                 = prodType;
    distinctProduct                             = new AxEcoResDistinctProduct();
    try
    {
        ttsbegin;
        ecoResProductName           = itemName;
        ecoResProductSearchName     = itemSearchName;
        languageId                  = "en-in";
        ecoResSize                  = EcoResSize::findByName(sizeCode);
        ecoResColor                 = EcoResColor::findByName(colorCode);
        ecoResConfiguration         = EcoResConfiguration::findByName(Config);
        ecoResStyle                 = EcoResStyle::findByName(style);
        if(dimGroupId)
        {
            // Code to insert in Product Master Tables
            if (!ecoResProduct)
            {
                // to type product Master
                axEcoResProductMaster = new AxEcoResProductMaster();
                axEcoResProductMaster.parmDisplayProductNumber(itemId);                
                axEcoResProductMaster.parmProductType(productType);
                axEcoResProductMaster.parmVariantConfigurationTechnology(ecoResVariantConfigurationTechnologyType);
                axEcoResProductMaster.parmSearchName(ecoResProductSearchName);
                axEcoResProductMaster.save();
                ecoResProduct = axEcoResProductMaster.ecoResProduct() ;
                ecoResProductIdentifier.initValue() ;
                ecoResProductIdentifier.ProductNumber   = itemId;
                ecoResProductIdentifier.Product         = ecoResProduct.RecId;
                ecoResProductIdentifier.insert() ;
                ecoResProductMaster = ecoResProduct as EcoResProductMaster;
                if (ecoResProductMaster.isDimensionBased() || ecoResProductMaster.isPredefinedVariants())
                {
                    ecoResProductMasterModelingPolicy.initValue() ;
                    ecoResProductMasterModelingPolicy.ProductMaster = ecoResProductMaster.RecId;
                    ecoResProductMasterModelingPolicy.insert() ;
                }
            }

            // Code for Size
            if(sizeCode)
            {
                if (!ecoResSize)
                {
                    ecoResSize.initValue();
                    ecoResSize.Name = sizeCode;
                    ecoResSize.insert();
                }

                select * from ecoResProductMasterSize
                    where ecoResProductMasterSize.SizeProductMaster == ecoResProduct.RecId
                        && ecoResProductMasterSize.Size             == ecoResSize.RecId;

                if (!ecoResProductMasterSize)
                {
                    axEcoResProductMasterSize = new AxEcoResProductMasterSize() ;
                    axEcoResProductMasterSize.parmSize(ecoResSize.RecId);
                    axEcoResProductMasterSize.parmSizeProductMaster(ecoResProduct.RecId);
                    axEcoResProductMasterSize.save();
                    ecoResProductMasterSize = axEcoResProductMasterSize.ecoResProductMasterSize() ;

                    ecoResProductMasterDimensionValue = axEcoResProductMasterSize.ecoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue = new AxEcoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue.ecoResProductMasterDimensionValue(ecoResProductMasterDimensionValue) ;
                    axEcoResProductMasterDimensionValue.doSave() ;

                    axEcoResProductMasterDimValueTranslation    = new axEcoResProductMasterDimValueTranslation();
                    axEcoResProductMasterDimValueTranslation.parmProductMasterDimensionValue(ecoResProductMasterDimensionValue.RecId);
                    axEcoResProductMasterDimValueTranslation.parmName(SizeCode);
                    axEcoResProductMasterDimValueTranslation.parmLanguageId(languageId);
                    axEcoResProductMasterDimValueTranslation.doSave();
                }
            }

            if (colorCode)
            {
                // Code for Color
                if (!ecoResColor)
                {
                    ecoResColor.initValue();
                    ecoResColor.Name = colorCode;
                    ecoResColor.insert();
                }
                select * from ecoResProductMasterColor
                    where ecoResProductMasterColor.ColorProductMaster   == ecoResProduct.RecId
                        && ecoResProductMasterColor.Color               == ecoResColor.RecId;

                if (!ecoResProductMasterColor)
                {
                    axEcoResProductMasterColor = new AxEcoResProductMasterColor() ;
                    axEcoResProductMasterColor.parmColor(ecoResColor.RecId);
                    axEcoResProductMasterColor.parmColorProductMaster(ecoResProduct.RecId);
                    axEcoResProductMasterColor.save();

                    ecoResProductMasterColor = axEcoResProductMasterColor.ecoResProductMasterColor() ;
                    ecoResProductMasterDimensionValue = axEcoResProductMasterColor.ecoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue = new AxEcoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue.ecoResProductMasterDimensionValue(ecoResProductMasterDimensionValue) ;
                    axEcoResProductMasterDimensionValue.doSave() ;


                    axEcoResProductMasterDimValueTranslation    = new axEcoResProductMasterDimValueTranslation();
                    axEcoResProductMasterDimValueTranslation.parmProductMasterDimensionValue(ecoResProductMasterDimensionValue.RecId);
                    axEcoResProductMasterDimValueTranslation.parmName(ColorName);
                    axEcoResProductMasterDimValueTranslation.parmLanguageId(languageId);
                    axEcoResProductMasterDimValueTranslation.doSave();
                }
            }

            if(Config)
            {
                //code for configuration
                if (!ecoResConfiguration)
                {
                    ecoResConfiguration.initValue();
                    ecoResConfiguration.Name = Config;
                    ecoResConfiguration.insert();
                }
                select * from ecoResProductMasterConfig
                        where ecoResProductMasterConfig.ConfigProductMaster == ecoResProduct.RecId
                        && ecoResProductMasterConfig.Configuration == ecoResConfiguration.RecId;

                if (!ecoResProductMasterConfig)
                {
                    axEcoResProductMasterConfiguration = new AxEcoResProductMasterConfiguration() ;
                    axEcoResProductMasterConfiguration.parmConfiguration(ecoResConfiguration.RecId);
                    axEcoResProductMasterConfiguration.parmConfigProductMaster(ecoResProduct.RecId);
                    axEcoResProductMasterConfiguration.save();
                    ecoResProductMasterConfig = axEcoResProductMasterConfiguration.ecoResProductMasterConfiguration() ;

                    ecoResProductMasterDimensionValue = axEcoResProductMasterConfiguration.ecoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue = new AxEcoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue.ecoResProductMasterDimensionValue(ecoResProductMasterDimensionValue) ;
                    axEcoResProductMasterDimensionValue.doSave() ;

                    axEcoResProductMasterDimValueTranslation    = new axEcoResProductMasterDimValueTranslation();
                    axEcoResProductMasterDimValueTranslation.parmProductMasterDimensionValue(ecoResProductMasterDimensionValue.RecId);
                    axEcoResProductMasterDimValueTranslation.parmName(config);
                    axEcoResProductMasterDimValueTranslation.parmLanguageId(languageId);
                    axEcoResProductMasterDimValueTranslation.doSave();
                }
            }

            if(style)
            {
                //code for Style
                if (!ecoResStyle)
                {
                    ecoResStyle.initValue();
                    ecoResStyle.Name = style;
                    ecoResStyle.insert();
                }
                select * from ecoResProductMasterStyle
                    where ecoResProductMasterStyle.StyleProductMaster == ecoResProduct.RecId
                    && ecoResProductMasterStyle.Style == ecoResStyle.RecId;

                if (!ecoResProductMasterStyle)
                {
                    axEcoResProductMasterStyle = new AxEcoResProductMasterStyle() ;
                    axEcoResProductMasterStyle.parmStyle(ecoResStyle.RecId);
                    axEcoResProductMasterStyle.parmStyleProductMaster(ecoResProduct.RecId);
                    axEcoResProductMasterStyle.save();
                    ecoResProductMasterStyle = axEcoResProductMasterStyle.ecoResProductMasterStyle() ;

                    ecoResProductMasterDimensionValue = axEcoResProductMasterStyle.ecoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue = new AxEcoResProductMasterDimensionValue() ;
                    axEcoResProductMasterDimensionValue.ecoResProductMasterDimensionValue(ecoResProductMasterDimensionValue) ;
                    axEcoResProductMasterDimensionValue.doSave() ;

                    axEcoResProductMasterDimValueTranslation    = new axEcoResProductMasterDimValueTranslation();
                    axEcoResProductMasterDimValueTranslation.parmProductMasterDimensionValue(ecoResProductMasterDimensionValue.RecId);
                    axEcoResProductMasterDimValueTranslation.parmName(style);
                    axEcoResProductMasterDimValueTranslation.parmLanguageId(languageId);
                    axEcoResProductMasterDimValueTranslation.doSave();
                }
            }
        }
        else //for type Product
        {

            if (!ecoResProduct)
            {
                distinctProduct = new AxEcoResDistinctProduct();
                distinctProduct.parmDisplayProductNumber(itemId);                
                distinctProduct.parmProductType(productType);
                distinctProduct.parmSearchName(itemSearchName);
                distinctProduct.save();
                ecoResProduct = distinctProduct.ecoResProduct() ;
            }
            ecoResProductIdentifier.initValue() ;
            ecoResProductIdentifier.ProductNumber   = itemId;
            ecoResProductIdentifier.Product         = ecoResProduct.RecId;
            select firstOnly ecoResProductIdentifierCheck
            where ecoResProductIdentifierCheck.ProductNumber == itemId;
            if (!ecoResProductIdentifierCheck)
            {
                ecoResProductIdentifier.insert() ;
            }
        }

        // code to insert Product Name
        ecoResProductTranslation = EcoResProductTranslation::findByProductLanguage(ecoResProduct.RecId, languageId) ;
        if (!ecoResProductTranslation)
        {
            axEcoResProductTranslation = new AxEcoResProductTranslation() ;
            axEcoResProductTranslation.parmLanguageId(languageId);
            axEcoResProductTranslation.parmName(ecoResProductName);
            axEcoResProductTranslation.parmProduct(ecoResProduct.RecId);
            axEcoResProductTranslation.parmDescription(ecoResProductName);
            axEcoResProductTranslation.save();
            ecoResProductTranslation = axEcoResProductTranslation.ecoResProductTranslation() ;
        }

        // Insert or Find Case for InventDim
        inventDimTOFind.InventSizeId    = sizeCode;
        inventDimTOFind.InventColorId   = colorCode;
        inventDimTOFind.configId        = Config;
        inventDimTOFind.InventStyleId   = Style;
        inventDim   = InventDim::findOrCreate(inventDimTOFind);

        // Code to insert Invent Tables
        inventTable = InventTable::find(itemId) ;
        if (!inventTable)
        {
            axinventTable = new AxInventTable();
            axInventTable.parmItemId(itemId);
            axinventTable.parmNameAlias(itemSearchName);
            axinventTable.parmPurchModel(NoYes::Yes);
            axinventTable.parmCostModel(NoYes::Yes);
            if(isCostVarient)
            {
                axinventTable.parmItemDimCostPrice(NoYes::Yes);
            }
            else
            {
                axinventTable.parmItemDimCostPrice(NoYes::No);
            }            
            axinventTable.parmProduct(ecoResProduct.RecId);
            axinventTable.parmBOMUnitId(BomUnitId);
            axinventTable.parmPmfProductType(pmfProductType);
            axinventTable.parmPackagingGroupId(packingUnit);
            axinventTable.parmTaxPackagingQty(packingQty);
            axinventTable.parmOrigCountryRegionId(origCountry);
            axinventTable.parmNetWeight(itemNetWeight);
            axinventTable.save();
            axinventTable.axInventTableModule_Invent().parmUnitId(unitId) ;
            axinventTable.axInventTableModule_Invent().save() ;
            axinventTable.axInventTableModule_Purch().parmUnitId(purchUnitId) ;
            axinventTable.axInventTableModule_Purch().save() ;
            axinventTable.axInventTableModule_Sales().parmUnitId(salesUnitId) ;            
            axinventTable.axInventTableModule_Purch().parmTaxItemGroupId(purchTaxItemGroup);
            axinventTable.axInventTableModule_Purch().parmOverDeliveryPct(overDeliveryPct);
            axinventTable.axInventTableModule_Sales().parmTaxItemGroupId(salesTaxItemGroup);
            axinventTable.axInventTableModule_Sales().save() ;
            inventTable = axinventTable.inventTable() ;
        }
        else
        {
            axinventTable = new AxInventTable();
            axinventTable.inventTable(inventTable);
        }

        // code to insert Product Dimension Group Product
        ecoResProductDimensionGroupProduct = EcoResProductDimensionGroupProduct::findByProduct(ecoResProduct.RecId);
        if (!ecoResProductDimensionGroupProduct && inventTable.ItemType == ItemType::Item && inventTable.productSubtype() == EcoResProductSubtype::ProductMaster)
        {
            axEcoResProductDimensionGroupProduct = new AxEcoResProductDimensionGroupProduct() ;
            axEcoResProductDimensionGroupProduct.parmProduct(ecoResProduct.RecId);
            axEcoResProductDimensionGroupProduct.parmProductDimensionGroup(ecoResProductDimensionGroup.RecId);
            axEcoResProductDimensionGroupProduct.save();
            ecoResProductDimensionGroupProduct = axEcoResProductDimensionGroupProduct.ecoResProductDimensionGroupProduct() ;
        }
        //code to insert Storage Dimension Group Product
        ecoResStorageDimensionGroupProduct = EcoResStorageDimensionGroupProduct::findByProduct(ecoResProduct.RecId);
        if (!ecoResStorageDimensionGroupProduct)
        {
            axEcoResStorageDimensionGroupProduct = new AxEcoResStorageDimensionGroupProduct() ;
            axEcoResStorageDimensionGroupProduct.parmProduct(ecoResProduct.RecId);
            axEcoResStorageDimensionGroupProduct.parmStorageDimensionGroup(ecoResStorageDimensionGroup.RecId);
            axEcoResStorageDimensionGroupProduct.save();
            ecoResStorageDimensionGroupProduct = axEcoResStorageDimensionGroupProduct.ecoResStorageDimensionGroupProduct() ;
        }

        ecoResStorageDimensionGroupItem = EcoResStorageDimensionGroupItem::findByItem(inventTable.dataAreaId,itemId);
        if (!ecoResStorageDimensionGroupItem)
        {
            axEcoResStorageDimensionGroupItem = new AxEcoResStorageDimensionGroupItem() ;
            axEcoResStorageDimensionGroupItem.axInventTable(axinventTable) ;
            axEcoResStorageDimensionGroupItem.parmItemDataAreaId(inventTable.dataAreaId) ;
            axEcoResStorageDimensionGroupItem.parmItemId(itemId);
            axEcoResStorageDimensionGroupItem.parmStorageDimensionGroup(ecoResStorageDimensionGroup.RecId);
            axEcoResStorageDimensionGroupItem.save();
            ecoResStorageDimensionGroupItem = axEcoResStorageDimensionGroupItem.ecoResStorageDimensionGroupItem() ;
        }
        // code to insert Tracking Dimension Group Product
        ecoResTrackingDimensionGroupProduct = EcoResTrackingDimensionGroupProduct::findByProduct(ecoResProduct.RecId);
        if (!ecoResTrackingDimensionGroupProduct)
        {
            axEcoResTrackingDimensionGroupProduct = new AxEcoResTrackingDimensionGroupProduct() ;
            axEcoResTrackingDimensionGroupProduct.parmProduct(ecoResProduct.RecId);
            axEcoResTrackingDimensionGroupProduct.parmTrackingDimensionGroup(ecoResTrackingDimensionGroup.RecId);
            axEcoResTrackingDimensionGroupProduct.save();
            ecoResTrackingDimensionGroupProduct = axEcoResTrackingDimensionGroupProduct.ecoResTrackingDimensionGroupProduct() ;
        }
        ecoResTrackingDimensionGroupItem = EcoResTrackingDimensionGroupItem::findByItem(inventTable.dataAreaId,itemId);
        if (!ecoResTrackingDimensionGroupItem)
        {
            axEcoResTrackingDimensionGroupItem = new AxEcoResTrackingDimensionGroupItem() ;
            axEcoResTrackingDimensionGroupItem.axInventTable(axinventTable) ;
            axEcoResTrackingDimensionGroupItem.parmItemDataAreaId(inventTable.dataAreaId) ;
            axEcoResTrackingDimensionGroupItem.parmItemId(itemId);
            axEcoResTrackingDimensionGroupItem.parmTrackingDimensionGroup(ecoResTrackingDimensionGroup.RecId);
            axEcoResTrackingDimensionGroupItem.save();
            ecoResTrackingDimensionGroupItem = axEcoResTrackingDimensionGroupItem.ecoResTrackingDimensionGroupItem() ;
        }
        // code to insert Item Model Group Product
        select * from inventModelGroupItem
            where inventModelGroupItem.ItemId                   == itemId
                && inventModelGroupItem.ModelGroupId            == modelGroupId
                && inventModelGroupItem.ItemDataAreaId          == inventTable.dataAreaId
                && inventModelGroupItem.ModelGroupDataAreaId    == inventTable.dataAreaId;
        if (!inventModelGroupItem)
        {
            axInventModelGroupItem = new AxInventModelGroupItem() ;
            axInventModelGroupItem.parmItemId(itemId);
            axInventModelGroupItem.parmModelGroupId(modelGroupId);
            axInventModelGroupItem.parmItemDataAreaId(inventTable.dataAreaId);
            axInventModelGroupItem.parmModelGroupDataAreaId(inventTable.dataAreaId);
            axInventModelGroupItem.save();
            inventModelGroupItem = axInventModelGroupItem.inventModelGroupItem() ;
        }
        select * from inventItemGroupItem
            where inventItemGroupItem.ItemDataAreaId        == inventTable.dataAreaId
                && inventItemGroupItem.ItemGroupDataAreaId  == inventTable.dataAreaId
                && inventItemGroupItem.ItemId               == itemId
                && inventItemGroupItem.ItemGroupId          == itemGroupId;
        if (!inventItemGroupItem)
        {
            axInventItemGroupItem = new AxInventItemGroupItem() ;
            axInventItemGroupItem.parmItemDataAreaId(inventTable.dataAreaId);
            axInventItemGroupItem.parmItemGroupDataAreaId(inventTable.dataAreaId);
            axInventItemGroupItem.parmItemId(itemId);
            axInventItemGroupItem.parmItemGroupId(itemGroupId);
            axInventItemGroupItem.save();
            inventItemGroupItem = axInventItemGroupItem.inventItemGroupItem() ;
        }
        if(dimGroupId)
        {
            // Code to Insert in Varaint Tables
            if (!ecoResDistinctProductVariant)
            {
                axEcoResDistinctProductVariant = new AxEcoResDistinctProductVariant();
                axEcoResDistinctProductVariant.parmProductType(ProductType);
                axEcoResDistinctProductVariant.parmProductMaster(ecoResProduct.RecId);
                axEcoResDistinctProductVariant.parmSearchName(ecoResProduct.SearchName);
                axEcoResDistinctProductVariant.parmDisplayProductNumber(ecoResProductDisplayProductNumberVariant);
                axEcoResDistinctProductVariant.parmRetaiTotalWeight(1);
                axEcoResDistinctProductVariant.save();
                ecoResDistinctProductVariant = axEcoResDistinctProductVariant.ecoResDistinctProductVariant() ;
            }
            // code to insert Product Variant Name
            select * from ecoResProductTranslation2
                    where  ecoResProductTranslation2.LanguageId == languageId
                    && ecoResProductTranslation2.Product        == ecoResDistinctProductVariant.RecId;
            if (!ecoResProductTranslation2)
            {
                variantName= "";
                variantName = itemName + " :: " + colorName + " : " + sizeCode;
                axEcoResProductTranslation2 = new AxEcoResProductTranslation();
                axEcoResProductTranslation2.parmLanguageId(languageId);                
                axEcoResProductTranslation2.parmName(variantName);
                axEcoResProductTranslation2.parmProduct(ecoResDistinctProductVariant.RecId);
                axEcoResProductTranslation2.parmDescription(itemName);
                axEcoResProductTranslation2.save();
                ecoResProductTranslation2 = axEcoResProductTranslation2.ecoResProductTranslation() ;
            }

            if(sizeCode)
            {
                // Code to Varaint Size
                select * from ecoResProductVariantSize
                    where ecoResProductVariantSize.DistinctProductVariant   == ecoResDistinctProductVariant.RecId
                    && ecoResProductVariantSize.Size                        == ecoResSize.RecId;
                if (!ecoResProductVariantSize)
                {
                    axEcoResProductVariantSize  = new AxEcoResProductVariantSize() ;
                    axEcoResProductVariantSize.parmSize(ecoResSize.RecId);
                    axEcoResProductVariantSize.parmDistinctProductVariant(ecoResDistinctProductVariant.RecId);
                    axEcoResProductVariantSize.save();
                    ecoResProductVariantSize = axEcoResProductVariantSize.ecoResProductVariantSize() ;
                }
            }
            if(colorCode)
            {
                // Code to Variant Color
                select * from ecoResProductVariantColor
                    where ecoResProductVariantColor.DistinctProductVariant  == ecoResDistinctProductVariant.RecId
                    && ecoResProductVariantColor.Color                      == ecoResColor.RecId;
                if (!ecoResProductVariantColor)
                {
                    axEcoResProductVariantColor = new AxEcoResProductVariantColor() ;
                    axecoResProductVariantColor.parmColor(ecoResColor.RecId);
                    axecoResProductVariantColor.parmDistinctProductVariant(ecoResDistinctProductVariant.RecId);
                    axecoResProductVariantColor.save();
                    ecoResProductVariantColor = axecoResProductVariantColor.ecoResProductVariantColor() ;
                }
            }
            if(Config)
            {
                // Code to Varaint Config
                select * from ecoResProductVariantConfiguration
                    where ecoResProductVariantConfiguration.DistinctProductVariant  == ecoResDistinctProductVariant.RecId
                        && ecoResProductVariantConfiguration.Configuration          == ecoResConfiguration.RecId;
                if (!ecoResProductVariantConfiguration)
                {
                    axEcoResProductVariantConfiguration  = new AxEcoResProductVariantConfiguration() ;
                    axEcoResProductVariantConfiguration.parmConfiguration(ecoResConfiguration.RecId);
                    axEcoResProductVariantConfiguration.parmDistinctProductVariant(ecoResDistinctProductVariant.RecId);
                    axEcoResProductVariantConfiguration.save();
                    ecoResProductVariantConfiguration = axEcoResProductVariantConfiguration.ecoResProductVariantConfiguration() ;
                }
            }
            if(Style)
            {
                // Code to Varaint Style
                select * from ecoResProductVariantStyle
                    where ecoResProductVariantStyle.DistinctProductVariant  == ecoResDistinctProductVariant.RecId
                        && ecoResProductVariantStyle.Style                  == ecoResStyle.RecId;
                if (!ecoResProductVariantStyle)
                {
                    axEcoResProductVariantStyle  = new AxEcoResProductVariantStyle() ;
                    axEcoResProductVariantStyle.parmStyle(ecoResStyle.RecId);
                    axEcoResProductVariantStyle.parmDistinctProductVariant(ecoResDistinctProductVariant.RecId);
                    axEcoResProductVariantStyle.save();
                    ecoResProductVariantStyle = axEcoResProductVariantStyle.ecoResProductVariantStyle() ;
                }
            }
            select * from inventDimCombination
                where inventDimCombination.ItemId       == itemId
                    && inventDimCombination.InventDimId == inventDim.inventDimId;
            if (!inventDimCombination)
            {
                axInventDimCombination = new AxInventDimCombination();
                axInventDimCombination.parmItemId(itemId);
                axInventDimCombination.parmInventDimId(inventDim.inventDimId);
                axInventDimCombination.parmDistinctProductVariant(ecoResDistinctProductVariant.RecId);
                axInventDimCombination.parmRetailVariantId(NumberSeq::newGetNumFromCode(InventDimCombination::numberSequenceCode()).num());
                axInventDimCombination.save();
                inventDimCombination = axInventDimCombination.inventDimCombination() ;
            }
        }
        // InterCompany External Codes
        extCodeValueTable = ExtCodeValueTable::find(TableNum(InventDimCombination), inventDimCombination.RecId, 'INT', true);
        if (extCodeValueTable)
        {
            extCodeValueTable.ExtCodeValue              = inventDimCombination.RetailVariantId;
            extCodeValueTable.update();
        }
        else
        {
            extCodeValueTable.initValue();
            extCodeValueTable.ExtCodeRelationTableId    = TableNum(InventDimCombination);
            extCodeValueTable.ExtCodeRelationRecId      = inventDimCombination.RecId;
            extCodeValueTable.ExtCodeId                 = 'INT';
            extCodeValueTable.ExtCodeValue              = inventDimCombination.RetailVariantId;
            extCodeValueTable.ExtCodeTableTableId       = TableNum(InventTable);
            extCodeValueTable.insert();
        }

        if (!RetailSeasonTable::find(seasonCode))
        {
            if (seasonCode != "")
            {
                retailSeasonTable.initValue();
                retailSeasonTable.seasonCode    = seasonCode;
                retailSeasonTable.Description   = seasonCode;
                retailSeasonTable.StartDate     = today();
                retailSeasonTable.insert();
            }

        }
        retailInventTable = RetailInventTable::find(itemId, True); // inventtable must exist. Ecoresproduct insertion inserts record in RetailInventTable also.
        retailInventTable.seasonCode            = seasonCode;
        retailInventTable.zeroPriceValid        = zeroPricevalid;
        retailInventTable.dateToActivateItem    = launchDate;
        retailInventTable.update();
        if (itemBarCode)
        {
            inventItemBarCode = InventItemBarcode::findBarcode(itemBarCode, false, false) ;
            if (!inventItemBarCode)
            {
                inventItemBarCode.initValue();
                inventItemBarCode.itemBarCode       = itemBarCode;
                inventItemBarCode.itemId            = itemId;
                if (variantName)
                    inventItemBarCode.description       = variantName;
                else
                    inventItemBarCode.description       = itemName;
                inventItemBarCode.inventDimId       = inventDim.inventDimId;
                inventItemBarCode.barcodeSetupId    = barCodeSetupId;
                inventItemBarCode.UnitID            = salesUnitId;
                inventItemBarCode.RetailVariantId   = inventDimCombination.RetailVariantId;                
                inventItemBarCode.useForInput       = NoYes::Yes;
                inventItemBarCode.useForPrinting    = NoYes::Yes;
                inventItemBarCode.qty               = 1;
                inventItemBarCode.insert();
            }
        }        

        inventTable.clear();
        inventTable = InventTable::find(itemId, true);        
        inventTable.PrimaryVendorId = defaultVendAccount;
        inventTable.SecurityCenters = securityCenter;
        inventTable.CostGroupId     = costGroupID;
        inventTable.projCategoryId  = projectId;
        inventTable.PmfYieldPct     = yieldPercent;
        inventTable.doupdate();
        ttsBegin;
        select forUpdate inventTableModule where inventTableModule.ItemId == itemId
        && inventTableModule.ModuleType == ModuleInventPurchSales::Purch;
        inventTableModule.LineDisc  = LineDiscCode;
        inventTableModule.Update();
        ttsCommit;

        //Financial Dimension - Item Group
        if (itemGroupId)
        {
            dimStorage = DimensionAttributeValueSetStorage::find(inventTable.DefaultDimension);
            FinanceDimensionAttribute = DimensionAttribute::findByName("ItemGroup");
            newFinanceValue = DimensionAttributeValue::findByDimensionAttributeAndValue(FinanceDimensionAttribute, itemGroupId, false, true);
            if (newFinanceValue)
            {
                dimStorage.addItem(newFinanceValue);
            }
            inventTable.DefaultDimension = dimStorage.save();
            inventTable.update();
        }
        //Financial Dimension - Segment
        if (segmentCode)
        {
            dimStorage = DimensionAttributeValueSetStorage::find(inventTable.DefaultDimension);
            FinanceDimensionAttribute = DimensionAttribute::findByName("Segment");
            newFinanceValue = DimensionAttributeValue::findByDimensionAttributeAndValue(FinanceDimensionAttribute, segmentCode, false, true);
            if (newFinanceValue)
            {
                dimStorage.addItem(newFinanceValue);
            }

            inventTable.DefaultDimension = dimStorage.save();
            inventTable.update();
        }
        ttscommit;
    }
    catch
    {
        Error(strfmt("Upload Failed in row %1", row));
    }

    info(strfmt("%1 records processed.", row - 1));
}

Method 6 run

public void run()
{
    this.importFromCSV();
}

Method 7 main

static client void  main(Args  args)
{
    Vv_ImportProductMaster     importProductMaster;

    importProductMaster = new Vv_ImportProductMaster() ;
    importProductMaster.run() ;
}

As always, please test this code in your UAT and pre-production servers before deploying in production server.

Happy Daxing!

Leave a Reply

Your email address will not be published. Required fields are marked *