Thursday, February 2, 2012

Release products to the legal entities using X++

In this post, I would like to share X++ Code that will help to release products to the legal entities from product master.

static void releaseProducts_Job(Args _args)
{
    EcoResProduct ecoResProduct;
    InventTable inventTable;
    InventTableModule inventTableModule;
    NumberSequenceTable numberSequenceTable;
    ItemId itemId;
    InventItemSetupSupplyType inventItemSetupSupplyType;

    EcoResStorageDimensionGroupProduct ecoResStorageDimensionGroupProduct;
    EcoResTrackingDimensionGroupProduct ecoResTrackingDimensionGroupProduct;
    EcoResStorageDimensionGroupItem ecoResStorageDimensionGroupItem;
    EcoResTrackingDimensionGroupItem ecoResTrackingDimensionGroupItem;
    ;

    select firstOnly ecoResProduct where EcoResProduct.DisplayProductNumber == "test"; //Audio system

    changecompany ('CEU')
    {
        ttsBegin;
        inventTable = null;
        inventTableModule = null;
        inventItemSetupSupplyType = null;
        ecoResStorageDimensionGroupProduct = null;
        ecoResTrackingDimensionGroupProduct = null;
        ecoResStorageDimensionGroupItem = null;
        ecoResTrackingDimensionGroupItem = null;

        numberSequenceTable = InventParameters::numRefItemId().numberSequenceTable();
        //get item id from:
        //1. Product number if number seq for item ID is not set up or manual or return blank value
        if (!numberSequenceTable.RecId || numberSequenceTable.Manual)
        {
            itemId = ecoResProduct.productNumber();
        }
        else //number sequence auto, get a number
        {
            itemId = NumberSeq::newGetNumFromId(numberSequenceTable.RecId).num();
        }

        inventTable.initValue();
        inventTable.initFromEcoResProduct(ecoResProduct);
        inventTable.ItemId = ItemId;
        inventTable.NameAlias = ecoResProduct.SearchName;
        inventTable.insert(true);

        // Create inventTableModules

        inventTableModule.initValue();
        inventTableModule.ItemId = inventTable.ItemId;
        inventTableModule.ModuleType = ModuleInventPurchSales::Invent;
        inventTableModule.insert();

        inventTableModule.initValue();
        inventTableModule.ItemId = inventTable.ItemId;
        inventTableModule.ModuleType = ModuleInventPurchSales::Purch;
        inventTableModule.insert();

        inventTableModule.initValue();
        inventTableModule.ItemId = inventTable.ItemId;
        inventTableModule.ModuleType = ModuleInventPurchSales::Sales;
        inventTableModule.insert();

        //Create inventItemLocation
        InventItemLocation::createDefault(inventTable.ItemId);

        // Creates a new item default order type for the product that is released.

        inventItemSetupSupplyType.initValue();
        inventItemSetupSupplyType.ItemId = inventTable.ItemId;
        inventItemSetupSupplyType.ItemDataAreaId = inventTable.DataAreaId;
        inventItemSetupSupplyType.insert();

        //create relationship tables to dimension groups.

        ecoResStorageDimensionGroupProduct = EcoResStorageDimensionGroupProduct::findByProduct(ecoResProduct.RecId);
        ecoResTrackingDimensionGroupProduct = EcoResTrackingDimensionGroupProduct::findByProduct(ecoResProduct.RecId);

        if (ecoResStorageDimensionGroupProduct.RecId)
        {  
            // mandatory storage dimension group for the product
            ecoResStorageDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
            ecoResStorageDimensionGroupItem.ItemId = inventTable.ItemId;
            ecoResStorageDimensionGroupItem.StorageDimensionGroup = ecoResStorageDimensionGroupProduct.StorageDimensionGroup;
            ecoResStorageDimensionGroupItem.insert();
        }

        if (ecoResTrackingDimensionGroupProduct.RecId)
        {
            // mandatory tracking dimension group for the product
            ecoResTrackingDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
            ecoResTrackingDimensionGroupItem.ItemId = inventTable.ItemId;
            ecoResTrackingDimensionGroupItem.TrackingDimensionGroup = ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup;
            ecoResTrackingDimensionGroupItem.insert();
        }
       
        ttsCommit;
       
        info("Product successfully released to CEU legal entity");
    }
}

2 comments:

Robert said...

The same can be done by the following code:


static void rmReleaseProductManager(Args _args)
{
EcoResProductRecId productRecId = EcoResProduct::findByDisplayProductNumber("ProductId").RecId;
RefRecId legalEntityId = CompanyInfo::findDataArea(curext()).RecId;
ItemId itemId = "ItemId";
ItemNameAlias itemNameAlias = "ItemAlias";
;

EcoResProductReleaseManager::releaseProductWithLegalEntityData(productRecId,
legalEntityId,
itemId,
itemNameAlias);
}

You also can use the InventItemService from c# or from x++

Ambanna Yatnal said...

Hi Robert,

Please can you share a X++ code sample to release a product and product variants using document service in AX 2012.

Thanks,
Ambanna Yatnal