Thursday 27 April 2017

Create Product Master with Product Varients Using X++

Public void Create_ProductMaster(ItemId _itemid,ItemName _itemname,Name _searchname,Name _name,ProductType _ProductType)
{
 EcoResProductMaster                    ecoResProductMaster;
 EcoResProductIdentifier                ecoResProductIdentifier;
 EcoResProductDimensionGroupProduct     ecoResProductDimensionGroupProduct;
 EcoResProductMasterModelingPolicy      ecoResProductMasterModelingPolicy;

 EcoResStorageDimensionGroupProduct     ecoResStorageDimensionGroupProduct;
 EcoResTrackingDimensionGroupProduct    ecoResTrackingDimensionGroupProduct;

 EcoResConfiguration                    ecoResConfiguration;
 EcoResProductMasterConfiguration       ecoResProductMasterConfiguration;

 EcoResSize                             ecoResSize;
 EcoResProductMasterSize                ecoResProductMasterSize;

 EcoResColor                            ecoResColor;
 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;

try
 {
     //ProductMaster
         ecoResProductMaster.clear();
         ecoResProductMaster.initValue();
         ecoResProductMaster.ProductType                    = EcoResProductType::Item;
         ecoResProductMaster.DisplayProductNumber           = _itemid;
         ecoResProductMaster.SearchName                     = _searchname;
         ecoResProductMaster.VariantConfigurationTechnology = EcoResVariantConfigurationTechnologyType::PredefinedVariants;
         if (ecoResProductMaster.validateWrite())
         {
            ecoResProductMaster.insert();
         }

     //Product Identifer
        ecoResProductIdentifier.clear();
        ecoResProductIdentifier.initValue();
        ecoResProductIdentifier.ProductNumber = _itemid;
        ecoResProductIdentifier.Product       = ecoResProductMaster.RecId;
        ecoResProductIdentifier.insert();

    //Product dimension group
        ecoResProductDimensionGroupProduct.clear();
        ecoResProductDimensionGroupProduct.initValue();
        ecoResProductDimensionGroupProduct.initFromProduct(ecoResProductMaster);
        ecoResProductDimensionGroupProduct.ProductDimensionGroup = EcoResProductDimensionGroup::findByDimensionGroupName("SizeColCon").RecId;
        if(ecoResProductDimensionGroupProduct.validateWrite())
        {
            ecoResProductDimensionGroupProduct.insert();
        }

    //Storage dimension group
        ecoResStorageDimensionGroupProduct.clear();
        ecoResStorageDimensionGroupProduct.initValue();
        ecoResStorageDimensionGroupProduct.Product               = ecoResProductMaster.RecId;
        ecoResStorageDimensionGroupProduct.StorageDimensionGroup = EcoResStorageDimensionGroup::findByDimensionGroupName("Site").RecId;
        if (ecoResStorageDimensionGroupProduct.validateWrite())
        {
            ecoResStorageDimensionGroupProduct.insert();
        }

    //Tracking dimension group
        ecoResTrackingDimensionGroupProduct.clear();
        ecoResTrackingDimensionGroupProduct.initValue();
        ecoResTrackingDimensionGroupProduct.Product                = ecoResProductMaster.RecId;
        ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup = EcoResTrackingDimensionGroup::findByDimensionGroupName("Serial").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, _itemname, _searchname);

    //Configuration
         ecoResConfiguration = EcoResConfiguration::findByName("NoteBook");
         if (!ecoResConfiguration)
         {
             ecoResConfiguration.clear();
             ecoResConfiguration.initValue();
             ecoResConfiguration.Name = "NoteBook ";
             ecoResConfiguration.insert();
         }

    //Size
         ecoResSize = EcoResSize::findByName("1");
         if(!ecoResSize)
         {
             ecoResSize.clear();
             ecoResSize.initValue();
             ecoResSize.Name = "1 ";
             ecoResSize.insert();
         }
    //color
        ecoResColor = EcoResColor::findByName("Black");
        if(!ecoResColor)
        {
            ecoResColor.clear();
            ecoResColor.initValue();
            ecoResColor.Name = "Black";
            ecoResColor.insert();
        }

    //InventDimId Creating
        inventDim.clear();
        inventDim.ConfigId = "NoteBook";/*ConfigId*/
        inventDim.InventSizeId =  "1";  //size
        inventDim.InventColorId = "Black"; // name
        inventDim = InventDim::findOrCreate(inventDim);

    //Configuration assigned to product master
        ecoResProductMasterConfiguration.clear();
        ecoResProductMasterConfiguration.initValue();
        ecoResProductMasterConfiguration.Configuration = ecoResConfiguration.RecId;
        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.SizeProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, InventSizeId));
        ecoResProductMasterSize.SizeProductMaster    = ecoResProductMaster.RecId;

    //color assigned to product master
        ecoResProductMasterColor.clear();
        ecoResProductMasterColor.initValue();
        ecoResProductMasterColor.Color = ecoResColor.RecId;
        ecoResProductMasterColor.ColorProductDimensionAttribute = EcoResProductDimensionAttribute::inventDimFieldId2DimensionAttributeRecId(fieldNum(InventDim, InventColorId));
        ecoResProductMasterColor.ColorProductMaster = ecoResProductMaster.RecId;

    //Product variant
        ecoResDistinctProductVariant.clear();
        ecoResDistinctProductVariant.initValue();
        ecoResDistinctProductVariant.DisplayProductNumber = EcoResProductNumberBuilderVariant::buildFromProductNumberAndDimensions(
        ecoResProductMaster.productNumber(),
        EcoResProductVariantDimValue::getDimensionValuesContainer(_itemname, "1", "Black",""));
        ecoResDistinctProductVariant.SearchName = ecoResProductMaster.SearchName /*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;
        ecoResProductVariantSize.insert();

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


    //Product variant translation
         EcoResProductTranslation::createOrUpdateTranslation(ecoResDistinctProductVariant.RecId, _itemname, _searchname);

    //Released product
        inventTable.clear();
        inventTable.initValue();
        inventTable.initFromEcoResProduct(ecoResProductMaster);
        inventTable.ItemId    =_itemid;
        inventTable.NameAlias =_searchname;
        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    = "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.insert();

    //Extended product details – Sales
        inventTableModule.clear();
        inventTableModule.initValue();
        inventTableModule.ItemId     = inventTable.ItemId;
        inventTableModule.ModuleType = ModuleInventPurchSales::Sales;
        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();
        }

    //Released product variant
        inventDimCombination.clear();
        inventDimCombination.initValue();
        inventDimCombination.DistinctProductVariant = ecoResDistinctProductVariant.RecId;
        inventDimCombination.ItemId                 = inventTable.ItemId;
        inventDimCombination.InventDimId            = inventDim.InventDimId;
        inventDimCombination.insert();
        info("Done!");
    }
   catch
   {
      error("Error!");
      return;
   }
}

No comments:

Post a Comment