Wednesday, July 25, 2018

Create and release product variant Dynamics 365

Code below can be used to create and release product variant in Dynamics AX 2012 & Dynamics 365 Operations.

It will work straight forwardly for AX 2012.

For Dynamics 365 operations,you need to create runable class to execute the same.

I have checked and tested that below code is working fine in dynamics 365 operations for different application but not to import data.

As all the classes and tables used below are available in Dynamics 365 operations, it should work to import as well.

Awaiting for feedback, if any!
 static void dtReleaseProductAllVariant(Args _args)  
 {  
   ecoResDistinctProductVariant    ecoResDistinctProductVariant;  
   EcoResProductVariantDimensionValue EcoResProductVariantDimensionValue;  
   RefRecId              ecoResDistinctProductVariantRecId;  
   EcoResProductReleaseManagerBase   releaseManager;  
   container              productDimensions;  
   EcoResProduct            ecoResProduct;  
   EcoResProductDimensionAttribute   prodDimensionAttribute; //fix for perticular dim, using tableid  
   //Configuration  
   EcoResProductMasterConfiguration  productMasterConfiguration;  
   EcoResConfiguration         ecoResConfiguration;  
   //Color  
   EcoResProductMasterColor      productMasterColor;  
   EcoResColor             ecoResColor;  
   //Size  
   EcoResProductMasterSize       productMasterSize;  
   EcoResSize             ecoResSize;  
   //Style  
   EcoResProductMasterStyle      productMasterStyle;  
   EcoResStyle             ecoResStyle;  
   CommaTextIo         file;  
   container          rec;  
   Dialog       d;  
   DialogField     df1, df2;  
   int64        counter = 0;  
   ;  
   d = new Dialog("Release Product Variant");  
   df1 = d.addField(ExtendedTypeStr("FilenameOpen"));  
   if (d.run())  
   {  
     file = new CommaTextIo(df1.value(), 'r');  
     file.inFieldDelimiter(';');  
     try  
     {  
       while (file.status() == IO_Status::Ok)  
       {  
         rec = file.read();  
         if(!rec)  
           break;  
           ttsBegin;  
           ecoResProduct = EcoResProduct::findByProductNumber(conPeek(rec, 1));  
           //Configuration - Start  
           ecoResConfiguration = EcoResConfiguration::findByName(conPeek(rec, 2));  
           if (!ecoResConfiguration.RecId)  
           {  
             ecoResConfiguration.Name = conPeek(rec, 2);  
             ecoResConfiguration.insert();  
           }  
           select * from productMasterConfiguration where productMasterConfiguration.Configuration == ecoResConfiguration.RecId &&  
                             productMasterConfiguration.ConfigProductMaster == ecoResProduct.RecId;  
           if(!productMasterConfiguration.RecId)  
           {  
             select * from prodDimensionAttribute where prodDimensionAttribute.DimensionTableId == tableNum(EcoResConfiguration);  
             productMasterConfiguration.Configuration              = ecoResConfiguration.RecId;  
             productMasterConfiguration.ConfigProductDimensionAttribute     = prodDimensionAttribute.RecId;  
             productMasterConfiguration.ConfigProductMaster           = ecoResProduct.RecId;  
             productMasterConfiguration.insert();  
           }  
           //Configuration - End  
           //Size - Start  
           ecoResSize = EcoResSize::findByName(conPeek(rec, 3));  
           if (!ecoResSize.RecId)  
           {  
             ecoResSize.Name = conPeek(rec, 3);  
             ecoResSize.insert();  
           }  
           select * from productMasterSize where productMasterSize.Size == ecoResSize.RecId &&  
                             productMasterSize.SizeProductMaster == ecoResProduct.RecId;  
           if(!productMasterSize.RecId)  
           {  
             select * from prodDimensionAttribute where prodDimensionAttribute.DimensionTableId == tableNum(EcoResSize);  
             productMasterSize.Size              = ecoResSize.RecId;  
             productMasterSize.SizeProductDimensionAttribute  = prodDimensionAttribute.RecId;  
             productMasterSize.SizeProductMaster        = ecoResProduct.RecId;  
             productMasterSize.insert();  
           }  
           //Size - End  
           //Color - Start  
           ecoResColor = EcoResColor::findByName(conPeek(rec, 4));  
           if (!ecoResColor.RecId)  
           {  
             ecoResColor.Name = conPeek(rec, 4);  
             ecoResColor.insert();  
           }  
           select * from productMasterColor where productMasterColor.color == ecoResColor.RecId &&  
                             productMasterColor.ColorProductMaster == ecoResProduct.RecId;  
           if(!productMasterColor.RecId)  
           {  
             select * from prodDimensionAttribute where prodDimensionAttribute.DimensionTableId == tableNum(EcoResColor);  
             productMasterColor.Color              = ecoResColor.RecId;  
             productMasterColor.ColorProductDimensionAttribute  = prodDimensionAttribute.RecId;  
             productMasterColor.ColorProductMaster        = ecoResProduct.RecId;  
             productMasterColor.insert();  
           }  
           //Color - End  
           //Style - Start  
           ecoResStyle = EcoResStyle::findByName(conPeek(rec, 5));  
           if (!ecoResStyle.RecId)  
           {  
             ecoResStyle.Name = conPeek(rec, 5);  
             ecoResStyle.insert();  
           }  
           select * from productMasterStyle where productMasterStyle.Style == ecoResStyle.RecId &&  
                             productMasterStyle.StyleProductMaster == ecoResProduct.RecId;  
           if(!productMasterStyle.RecId)  
           {  
             select * from prodDimensionAttribute where prodDimensionAttribute.DimensionTableId == tableNum(EcoResStyle);  
             productMasterStyle.Style              = ecoResStyle.RecId;  
             productMasterStyle.StyleProductDimensionAttribute  = prodDimensionAttribute.RecId;  
             productMasterStyle.StyleProductMaster        = ecoResProduct.RecId;  
             productMasterStyle.insert();  
           }  
           //Style - End  
           //Create a container to hold dimension values  
           productDimensions = EcoResProductVariantDimValue::getDimensionValuesContainer(conPeek(rec, 2),  
                                                   conPeek(rec, 3),  
                                                   conPeek(rec, 4),  
                                                   conPeek(rec, 5));  
           //Create Product search name  
           ecoResDistinctProductVariant.DisplayProductNumber = EcoResProductNumberBuilderVariant::buildFromProductNumberAndDimensions(  
                                             conPeek(rec, 1),  
                                             productDimensions);  
           //Create Product variant with Product and dimensions provided  
           ecoResDistinctProductVariantRecId = EcoResProductVariantManager::createProductVariant(ecoResProduct.RecId,  
                                             ecoResDistinctProductVariant.DisplayProductNumber,  
                                             productDimensions);  
           //Find newly created Product Variant  
           ecoResDistinctProductVariant = ecoResDistinctProductVariant::find(ecoResDistinctProductVariantRecId);  
           //Now release the Product variant  
           releaseManager = EcoResProductReleaseManagerBase::newFromProduct(ecoResDistinctProductVariant);  
           releaseManager.release();  
           counter++;  
           ttsCommit;  
         info(strFmt("%1 Product variant released successfully",conPeek(rec, 1)));  
         info(strFmt("Total %1 Product variants released successfully",counter));  
       }  
     }  
     catch  
     {  
       info(strFmt("Total %1 Product variants released successfully",counter));  
       info(strFmt("Job terminated for product %1 and config %2 Size %3 Color %4 Style %5",conPeek(rec, 1),conPeek(rec, 2),conPeek(rec, 3),conPeek(rec, 4),conPeek(rec, 5)));  
     }  
   }  
 }  

1 comment:

  1. Hi,

    Thats a good post, it helped me but a slight change is required while you are creating the Product variant record, you have to first check the condition if the record already exist becaust while creating the product master disctinct product gets created along with its dimension,
    i made the change and it worked.
    if(EcoResProductVariantManager::existDistinctProductVariant(ecoResProduct.RecId,productDimensions))
    {
    ecoResDistinctProductVariantRecId = EcoResProductVariantManager::findDistinctProductVariant(ecoResProduct.RecId,productDimensions).RecId;
    }
    else
    {
    ecoResDistinctProductVariantRecId = EcoResProductVariantManager::createProductVariant(ecoResProduct.RecId,
    ecoResDistinctProductVariant.DisplayProductNumber,
    productDimensions);
    }

    ReplyDelete

Happening

Upgrade from AX 2012 to Latest Dynamics 365 Finance and Operation

Below are the steps defined by sequence. 1. Create new Upgrade project in Dynamics LCS. 2. Create VSTS Project and connect it with L...

Trending now