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");
    }
}
Post a Comment