Showing posts with label Product. Show all posts
Showing posts with label Product. Show all posts

Sunday, December 8, 2013

How to convert Product master to Product in Ax2012


In One of my implementation I was having a requirement in this I had to convert product master to product across the companies. You can find in below code how we can do this.


public void convertProductMasterToProduct(ItemId    _itemId,
                                          str       _supplyCompany = "",
                                          str       _demandCompany = "")
{
    EcoResProductMaster             master;
    EcoResDistinctProduct           distinctProduct;
    EcoResProductTranslation        productTranslation;
    InventTable                     inventtable;
    EcoResProductIdentifier         productIdentifier;
    str                             text;
    str                             shortText;
    ;

    ttsBegin;

    master = EcoResProductMaster::find(inventtable::find(_itemId).Product, true);

    if (master)
    {
        distinctProduct.clear();
        distinctProduct.initValue();
        distinctProduct.DisplayProductNumber = master.DisplayProductNumber;
        distinctProduct.SearchName = master.SearchName;
        distinctProduct.ProductType = EcoResProductType::Item;
        text = EcoResProductTranslation::findByProductLanguage(master.RecId, CompanyInfo::languageId()).Description;
        shortText = EcoResProductTranslation::findByProductLanguage(master.RecId, CompanyInfo::languageId()).Name;
        master.delete();
        distinctProduct.insert();

       productTranslation.clear();
       productTranslation.initValue();
       productTranslation.LanguageId = CompanyInfo::find().LanguageId;
       productTranslation.Name = shortText;
       productTranslation.Product = distinctProduct.RecId;
       productTranslation.Description = text;
       productTranslation.insert();

       productIdentifier.clear();
       productIdentifier.initValue();
       productIdentifier.ProductNumber = _itemId;
       productIdentifier.Product = distinctProduct.RecId;
       productIdentifier.insert();
        if (_supplyCompany)
        {
            changeCompany(_supplyCompany)
            {
                inventTable = InventTable::find(_itemId, true);
                if (inventtable)
                {
                    InventTable.Product = distinctProduct.RecId;
                    inventtable.StandardConfigId = "";
                    InventTable.update();
                }
            }
        }
        if (_demandCompany)
        {
            changeCompany(_demandCompany)
            {
                inventTable = InventTable::find(_itemId, true);
                if (inventtable)
                {
                    InventTable.Product = distinctProduct.RecId;
                    inventtable.StandardConfigId = "";
                    InventTable.update();

                }
            }
        }
    }
    ttsCommit;


}