3

In AX 2012 we used to create Product and Product master easily through coding by use of some classes like:

 ecoresProductService  = EcoResProductService::construct();
 ecoResEcoResProduct   = new EcoResEcoResProduct();
 distintMaster         = new EcoResEcoResProduct_Product_Distinct();

These classes do not exist in AX 365. I need to create released Product through coding. Please share if you know how to create. Thanks in advance.

Yasir
  • 71
  • 2
  • 7

2 Answers2

3

I guess that with AX 2012 it is easier to create and release a product by using X++ code, but it is possibile to obtain the same result with AX7 (or dynamics 365 if you prefer).

The idea is to use the product data entity (i.e. EcoResProductEntity) and some standard - long named - classes.

Here is the code:

EcoResProductEntity                        ecoResProductEntity;

EcoResProductEntityToCrossTableDataAdaptor adaptor;
EcoResProduct                              product;

NumberSequenceReference                    numberSequenceReference = EcoResProductParameters::numRefProductNumber();
NumberSequenceTable                        numberSequenceTable = numberSequenceReference.numberSequenceTable();

Args                                       args;

NumberSeq                                  numberSeq = NumberSeq::newGetNumFromId(numberSequenceTable.RecId);

EcoResProductReleaseSessionManager         productReleaseSessionManager;
EcoResReleaseSessionRecId                  releaseSessionRecId;

CompanyInfo                                companyInfo = CompanyInfo::find();


ecoResProductEntity.ProductNumber                   = numberSeq.num();
ecoResProductEntity.ProductSearchName               = "myItem";
ecoResProductEntity.ProductName                     = "My Item";
ecoResProductEntity.ProductType                     = EcoResProductType::Item;
ecoResProductEntity.ProductSubType                  = EcoResProductSubtype::ProductMaster;
ecoResProductEntity.VariantConfigurationTechnology  = EcoResVariantConfigurationTechnologyType::PredefinedVariants;
ecoResProductEntity.ProductDimensionGroupName       = "Prod_Dim";

// here you can set all the fields of the data entity that you need

adaptor = EcoResProductEntityToCrossTableDataAdaptor::newFromEntity(ecoResProductEntity);

ttsbegin;

product = EcoResProductCrossTableManager::makeProductRecord(adaptor);

EcoResProductCrossTableManager::insert(adaptor, product);
// here you can create one or more translations
EcoResProductTranslation::createOrUpdateTranslation(product.RecId, "it translation", '', "it");

// now we want to release that master product for the current company    
productReleaseSessionManager    = EcoResProductReleaseSessionManager::newReleaseSession();
releaseSessionRecId             = productReleaseSessionManager.parmReleaseSessionRecId();

productReleaseSessionManager.addProduct(product.RecId);
productReleaseSessionManager.addLegalEntityForProduct(companyInfo.RecId, product.RecId);

args = new Args(formStr(EcoResProductRelease));
args.record(EcoResReleaseSession::find(releaseSessionRecId));

// the first boolean parameter is for showing a log for errors
// the second boolean parameter is for executing the release with a batch          
if (EcoResProductReleaseSessionBatch::runJob(args, true, false))
{
    productReleaseSessionManager.cleanUp();
}

ttscommit;

I hope it can help you

Il Vic
  • 5,576
  • 4
  • 26
  • 37
0

Thanks Il Vic this solution is perfect for creating a released product. I had tried this out first but found that "EcoResProductEntityToCrossTableDataAdaptor" has implemented an interface "EcoResIProductCrossTableData". If we go through implementation of "EcoResProductEntityToCrossTableDataAdaptor"class we find that many important parm method are not allowed to be called. The method are implemented to throw errors. So only one choice lefts, to implement the interface "EcoResIProductCrossTableData" yourself. I did and worked like a charm.

Yasir
  • 71
  • 2
  • 7
  • Hi @Yasir, I know some parms in the `EcoResProductEntityToCrossTableDataAdaptor` class can't be called, but may I ask you which one (or maybe ones) do you mean? Probably you can obtain the same result by setting the corresponding value in the `EcoResProductEntity`. – Il Vic May 12 '17 at 13:44