Saturday, 11 July 2015

Product Master Import through Job in ax 2012

/Excel file import using X++ Code
static void MyJob(Args _args)
{
#AviFiles
SysOperationProgress progress = new SysOperationProgress();
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelWorkSheet workSheetInventTableModule;
SysExcelCells cellsInventTableModule;
SysExcelCells cells;
COMVariantType type;
COMVariantType typeModule;
//ItemMasterRMVar2 itemMastervar;
    FileName                            filename;
    str     strsize,strcolor;
    int row = 1;
    EcoResProductMaster                 ecoResProductMaster;
    EcoResProductIdentifier             ecoResProductIdentifier;
    EcoResProductDimensionGroupProduct  ecoResProductDimensionGroupProduct;
    EcoResProductMasterModelingPolicy   ecoResProductMasterModelingPolicy;

    EcoResStorageDimensionGroupProduct  ecoResStorageDimensionGroupProduct;
    EcoResTrackingDimensionGroupProduct ecoResTrackingDimensionGroupProduct;
    str dimgroup ,storgeGroup,TrackingGroup;

    EcoResConfiguration                 ecoResConfiguration;
    EcoResSize                          ecoResSize;//
    EcoResColor                         ecoResColor;//

    EcoResProductMasterConfiguration    ecoResProductMasterConfiguration;
    EcoResProductMasterSize             ecoResProductMasterSize;//
    EcoResProductMastercolor            ecoResProductMastercolor;//

    EcoResDistinctProductVariant        ecoResDistinctProductVariant;
    EcoResProductVariantConfiguration   ecoResProductVariantConfiguration;
    EcoResProductVariantColor           ecoResProductVariantColor;//
    EcoResProductVariantSize            ecoResProductVariantSize;//

    InventTable                         inventTable;
    InventTableModule                   inventTableModule;
    InventItemSetupSupplyType           inventItemSetupSupplyType;

    EcoResStorageDimensionGroupItem     ecoResStorageDimensionGroupItem;
    EcoResTrackingDimensionGroupItem    ecoResTrackingDimensionGroupItem;

    InventModelGroupItem                inventModelGroupItem;
    InventItemGroupItem                 inventItemGroupItem;

    InventDim                           inventDim;
    InventDimCombination                inventDimCombination;
FileIoPermission perm;
    inventTable   inventTable_loc;

application = SysExcelApplication::construct();
workbooks   = application.workbooks();

filename = "E:\\import\\PIM-Imported - Copy.xlsx";
ttsBegin;

try
{
workbooks.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
row++;
if (row > 1)
{
    try
    {
        //ProductMaster
        ecoResProductMaster.clear();
        ecoResProductMaster.initValue();

        ecoResProductMaster.ProductType = EcoResProductType::Item;
        ecoResProductMaster.DisplayProductNumber = int2str(cells.item(row, 1).value().double());
        ecoResProductMaster.SearchName = cells.item(row, 4).value().bStr();
        ecoResProductMaster.VariantConfigurationTechnology = EcoResVariantConfigurationTechnologyType::PredefinedVariants;

        if (ecoResProductMaster.validateWrite())
        {
            ecoResProductMaster.insert();

            ecoResProductIdentifier.clear();
            ecoResProductIdentifier.initValue();

            ecoResProductIdentifier.ProductNumber =  int2str(cells.item(row, 1).value().double());
            ecoResProductIdentifier.Product = ecoResProductMaster.RecId;

            ecoResProductIdentifier.insert();

            //Product dimension group
            ecoResProductDimensionGroupProduct.clear();
            ecoResProductDimensionGroupProduct.initValue();
            ecoResProductDimensionGroupProduct.initFromProduct(ecoResProductMaster);

             dimGroup=cells.item(row, 7).value().bStr();
           // ecoResProductDimensionGroupProduct.ProductDimensionGroup  =dimgroup.
            ecoResProductDimensionGroupProduct.ProductDimensionGroup = EcoResProductDimensionGroup::findByDimensionGroupName(dimgroup).RecId;

            if (ecoResProductDimensionGroupProduct.validateWrite())
            {
                ecoResProductDimensionGroupProduct.insert();
            }

            //Storage dimension group
            ecoResStorageDimensionGroupProduct.clear();
            ecoResStorageDimensionGroupProduct.initValue();
            ecoResStorageDimensionGroupProduct.Product = ecoResProductMaster.RecId;
           storgeGroup=cells.item(row, 16).value().bStr();
            ecoResStorageDimensionGroupProduct.StorageDimensionGroup = EcoResStorageDimensionGroup::findByDimensionGroupName(storgeGroup).RecId;

            if (ecoResStorageDimensionGroupProduct.validateWrite())
            {
                ecoResStorageDimensionGroupProduct.insert();
            }

            //Tracking dimension group
            ecoResTrackingDimensionGroupProduct.clear();
            ecoResTrackingDimensionGroupProduct.initValue();
            ecoResTrackingDimensionGroupProduct.Product = ecoResProductMaster.RecId;
             TrackingGroup="None";//cells.item(row, 17).value().bStr();

            ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup = EcoResTrackingDimensionGroup::findByDimensionGroupName(TrackingGroup).RecId;

            if (ecoResTrackingDimensionGroupProduct.validateWrite())
            {
                ecoResTrackingDimensionGroupProduct.insert();
            }

            //Product modeling policy
            ecoResProductMasterModelingPolicy.clear();
            ecoResProductMasterModelingPolicy.initValue();

            ecoResProductMasterModelingPolicy.ProductMaster = ecoResProductMaster.RecId;

            if (ecoResProductMasterModelingPolicy.validateWrite())
            {
                ecoResProductMasterModelingPolicy.insert();
            }

            //Product translation
          //  EcoResProductTranslation::createOrUpdateTranslation(ecoResProductMaster.RecId, ecoResProductIdentifier.ProductNumber, ecoResProductIdentifier.ProductNumber);
            EcoResProductTranslation::createOrUpdateTranslation(ecoResProductMaster.RecId,ecoResProductMaster.SearchName, ecoResProductMaster.SearchName);

            //Configuration
            //ecoResConfiguration = EcoResConfiguration::findByName(ecoResProductMaster.SearchName);
//
            //if (!ecoResConfiguration)
            //{
                //ecoResConfiguration.clear();
                //ecoResConfiguration.initValue();
//
                //ecoResConfiguration.Name =ecoResProductMaster.SearchName;//"Noor";//cells.item(row, 9).value().toString() ;
              ////  ecoResConfiguration.insert();
            //}
//
            ////Configuration assigned to product master
            //ecoResProductMasterConfiguration.clear();
            //ecoResProductMasterConfiguration.initValue();
            //ecoResProductMasterConfiguration.Configuration = ecoResConfiguration.RecId;
            //ecoResProductMasterConfiguration.Description = ecoResConfiguration.Name;//"Alex-1";
            //ecoResProductMasterConfiguration.ConfigProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, ConfigId));
            //ecoResProductMasterConfiguration.ConfigProductMaster = ecoResProductMaster.RecId;
         ////   ecoResProductMasterConfiguration.insert();

            /////////////////////
            //size
            strsize = cells.item(row, 2).value().bStr();
            ecoRessize = EcoRessize::findByName(strsize);//ecoResProductMaster.SearchName);

             if (!ecoRessize)
            {
                ecoRessize.clear();
                ecoRessize.initValue();

                ecoRessize.Name =cells.item(row, 2).value().bStr();//ecoResProductMaster.SearchName;//"Noor";//cells.item(row, 9).value().toString() ;
                if(ecoRessize.Name)
                ecoRessize.insert();
            }

            /////////
/// color
             strcolor = cells.item(row, 3).value().bStr();
             ecoRescolor = ecoRescolor::findByName(strcolor);//ecoResProductMaster.SearchName);

             if (!ecoRescolor)
            {
                ecoRescolor.clear();
                ecoRescolor.initValue();

                ecoRescolor.Name =cells.item(row, 3).value().bStr();//ecoResProductMaster.SearchName;//"Noor";//cells.item(row, 9).value().toString() ;
                if(ecoRescolor.Name)
                ecoRescolor.insert();
            }


            ///////

            //Configuration assigned to product master
            //ecoResProductMasterConfiguration.clear();
            //ecoResProductMasterConfiguration.initValue();
            //ecoResProductMasterConfiguration.Configuration = ecoResConfiguration.RecId;
            //ecoResProductMasterConfiguration.Description = ecoResConfiguration.Name;//"Alex-1";
            //ecoResProductMasterConfiguration.ConfigProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, ConfigId));
            //ecoResProductMasterConfiguration.ConfigProductMaster = ecoResProductMaster.RecId;
          ////  ecoResProductMasterConfiguration.insert();

/// /////////
/// size assigned to product master

            ecoResProductMastersize.clear();
            ecoResProductMastersize.initValue();
            ecoResProductMastersize.Size = ecoRessize.RecId;
            ecoResProductMastersize.Description = ecoRessize.Name;//"Alex-1";
            ecoResProductMastersize.sizeProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, InventSizeId));
            ecoResProductMastersize.sizeProductMaster = ecoResProductMaster.RecId;
            if(ecoRessize.Name)
            ecoResProductMastersize.insert();


/// color assigned to product master

            ecoResProductMasterColor.clear();
            ecoResProductMasterColor.initValue();
            ecoResProductMasterColor.Color = ecoResColor.RecId;
            ecoResProductMasterColor.Description = ecoResColor.Name;//"Alex-1";
            ecoResProductMasterColor.ColorProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, InventColorId));
            ecoResProductMasterColor.ColorProductMaster = ecoResProductMaster.RecId;
            if(ecoRescolor.Name)
            ecoResProductMasterColor.insert();

  //////      /////    /////////

            //Product variant
            ecoResDistinctProductVariant.clear();
            ecoResDistinctProductVariant.initValue();
            ecoResDistinctProductVariant.DisplayProductNumber = EcoResProductNumberBuilderVariant::buildFromProductNumberAndDimensions(
                ecoResProductMaster.productNumber(),
                EcoResProductVariantDimValue::getDimensionValuesContainer(ecoResConfiguration.Name, ecoressize.Name, ecoResColor.Name));
            ecoResDistinctProductVariant.SearchName = ecoResProductMaster.SearchName;// + ecoResConfiguration.Name;//"Alex-1"/*ConfigId*/;
            ecoResDistinctProductVariant.ProductType = ecoResProductMaster.ProductType;
            ecoResDistinctProductVariant.ProductMaster = ecoResProductMaster.RecId;
            ecoResDistinctProductVariant.insert();

            //Product variant configuration
            //ecoResProductVariantConfiguration.clear();
            //ecoResProductVariantConfiguration.initValue();
            //ecoResProductVariantConfiguration.initFromDistinctProductVariant(ecoResDistinctProductVariant);
            //ecoResProductVariantConfiguration.ProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, ConfigId));
            //ecoResProductVariantConfiguration.Configuration = ecoResConfiguration.RecId;
          ////  ecoResProductVariantConfiguration.insert();

            //////////
///Product variant size
            ecoResProductVariantsize.clear();
            ecoResProductVariantsize.initValue();
            ecoResProductVariantsize.initFromDistinctProductVariant(ecoResDistinctProductVariant);
            ecoResProductVariantsize.ProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, InventSizeId));
            ecoResProductVariantsize.Size = ecoRessize.RecId;
            if(ecoRessize.Name)
            ecoResProductVariantsize.insert();


///Product variant color
            ecoResProductVariantColor.clear();
            ecoResProductVariantColor.initValue();
            ecoResProductVariantColor.initFromDistinctProductVariant(ecoResDistinctProductVariant);
            ecoResProductVariantColor.ProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, InventColorId));
            ecoResProductVariantColor.Color = ecoResColor.RecId;
            if(ecoRescolor.Name)
            ecoResProductVariantColor.insert();


/// ///////

            //Product variant translation
            EcoResProductTranslation::createOrUpdateTranslation(ecoResDistinctProductVariant.RecId,ecoResProductMaster.SearchName, ecoResProductMaster.SearchName);
/////////////////////
            //Released product
            inventTable.clear();
            inventTable.initValue();

            inventTable.initFromEcoResProduct(ecoResProductMaster);
/////////////////////
            inventTable.ItemId = ecoResProductIdentifier.ProductNumber;//"AlexMaster";
            inventTable.NameAlias = ecoResProductMaster.SearchName;// ecoResProductIdentifier.ProductNumber;//"AlexMaster";
           // inventTable.StandardInventColorId = cells.item(row, 3).value().bStr();
          //  inventTable.StandardInventSizeId = cells.item(row, 2).value().bStr();
            inventTable.PmfProductType = PmfProductType::None;

            if (inventTable.validateWrite())
            {
                inventTable.insert();

                //Inventory model group
                inventModelGroupItem.clear();
                inventModelGroupItem.initValue();
                inventModelGroupItem.ItemDataAreaId = inventTable.dataAreaId;
                inventModelGroupItem.ItemId = inventTable.ItemId;
                inventModelGroupItem.ModelGroupId = "FIFO";
                inventModelGroupItem.ModelGroupDataAreaId = curext();
                inventModelGroupItem.insert();

                //Item group
                inventItemGroupItem.clear();
                inventItemGroupItem.initValue();
                inventItemGroupItem.ItemDataAreaId = inventTable.dataAreaId;
                inventItemGroupItem.ItemId = inventTable.ItemId;
                inventItemGroupItem.ItemGroupId =  cells.item(row, 8).value().bStr();//"Parts";
                inventItemGroupItem.ItemGroupDataAreaId = curext();
                inventItemGroupItem.insert();

                //Extended product details - Inventory
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId = inventTable.ItemId;
                inventTableModule.ModuleType = ModuleInventPurchSales::Invent;
                inventTableModule.insert();

                //Extended product details - Purchase
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId = inventTable.ItemId;
                inventTableModule.ModuleType = ModuleInventPurchSales::Purch;
                inventTableModule.UnitId = cells.item(row, 9).value().bStr();
                inventTableModule.insert();

                //Extended product details - Sales
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId = inventTable.ItemId;
                inventTableModule.ModuleType = ModuleInventPurchSales::Sales;
                inventTableModule.UnitId = cells.item(row, 10).value().bStr();
                inventTableModule.insert();

                //Warehouse items
                InventItemLocation::createDefault(inventTable.ItemId);

                //Supply type setup
                inventItemSetupSupplyType.clear();
                inventItemSetupSupplyType.initValue();
                inventItemSetupSupplyType.ItemId = inventTable.ItemId;
                inventItemSetupSupplyType.ItemDataAreaId = inventTable.DataAreaId;
                inventItemSetupSupplyType.insert();

                //Product storage dimension group
                ecoResStorageDimensionGroupProduct = EcoResStorageDimensionGroupProduct::findByProduct(ecoResProductMaster.RecId);

                if (ecoResStorageDimensionGroupProduct.RecId)
                {
                    ecoResStorageDimensionGroupItem.clear();
                    ecoResStorageDimensionGroupItem.initValue();
                    ecoResStorageDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
                    ecoResStorageDimensionGroupItem.ItemId = inventTable.ItemId;
                    ecoResStorageDimensionGroupItem.StorageDimensionGroup   = ecoResStorageDimensionGroupProduct.StorageDimensionGroup;
                    ecoResStorageDimensionGroupItem.insert();
                }

                //Product tracking dimension group
                ecoResTrackingDimensionGroupProduct = EcoResTrackingDimensionGroupProduct::findByProduct(ecoResProductMaster.RecId);

                if (ecoResTrackingDimensionGroupProduct.RecId)
                {
                    ecoResTrackingDimensionGroupItem.clear();
                    ecoResTrackingDimensionGroupItem.initValue();
                    ecoResTrackingDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
                    ecoResTrackingDimensionGroupItem.ItemId = inventTable.ItemId;
                    ecoResTrackingDimensionGroupItem.TrackingDimensionGroup = ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup;
                    ecoResTrackingDimensionGroupItem.insert();
                }
            }

            inventDim.clear();
            inventDim.ConfigId =ecoResConfiguration.Name;//ecoResProductMaster.SearchName;//"Noor" ;//ecoResConfiguration.Name;//"Alex-1";/*ConfigId*/
            inventDim.InventSizeId = ecoResSize.Name;
            inventDim.InventColorId = ecoResColor.Name;

            inventDim = InventDim::findOrCreate(inventDim);

            //Released product variant
            inventDimCombination.clear();
            inventDimCombination.initValue();
            inventDimCombination.DistinctProductVariant = ecoResDistinctProductVariant.RecId;
            inventDimCombination.ItemId = inventTable.ItemId;
            inventDimCombination.InventDimId = inventDim.InventDimId;
            inventDimCombination.insert();
        }
        //////////
///
        select forUpdate inventTable_loc where inventTable_loc.ItemId == inventTable.ItemId;
        {
            inventTable_loc.StandardInventColorId = cells.item(row, 3).value().bStr();
            inventTable_loc.StandardInventSizeId = cells.item(row, 2).value().bStr();

            if (inventTable_loc.validateWrite())
            {
                inventTable_loc.update();
            }
        }
    }
    catch
    {
        error("Error!");
        return;
    }
}
                   else
                {
                    throw warning(' does not exist  item master.' );
                }
            type = cells.item(row+1, 1).value().variantType();
            }



        while (type != COMVariantType::VT_EMPTY);
    application.quit();
    info("Done");
        ttscommit;
    }



//catch (Exception::Error)
//{
//workbooks.close();
//CodeAccessPermission::revertAssert();
//application.quit();
//ttsabort;
//}
//workbooks.close();
//CodeAccessPermission::revertAssert();

2 comments:

  1. why when I try to run the Job there are fields that must be filled in?

    ReplyDelete