AX – D365FO – How to create an Item/Product and release in specific company using X++ code

In one of my implementation I had requirement in this I had to create an Item in Ax using x++ code. Below code explains how we can create item in AX 2012 in a specific company.

//Creation of Product and release
static void ProductsXppImport(Args _args)
{
    #define.ProductNum()
    #define.SearchName()
    #define.StorageDim()
    #define.TrackingDim()
    #define.ItemId()
    #define.NameAlias()
    #define.ModelGroup()
    #define.ItemGroup()
    EcoResDistinctProduct               ecoResDistinctProduct;
    EcoResProductIdentifier             ecoResProductIdentifier;
    EcoResStorageDimensionGroupProduct  ecoResStorageDimensionGroupProduct;
    EcoResTrackingDimensionGroupProduct ecoResTrackingDimensionGroupProduct;
    InventTable                         inventTable;
    InventTableModule                   inventTableModule;
    InventItemSetupSupplyType           inventItemSetupSupplyType;
    EcoResStorageDimensionGroupItem     ecoResStorageDimensionGroupItem;
    EcoResTrackingDimensionGroupItem    ecoResTrackingDimensionGroupItem;
    InventModelGroupItem                inventModelGroupItem;
    InventItemGroupItem                 inventItemGroupItem;
    
    EcoResProductReleaseManagerBase     releaseManager;
    try
    {
        //Product
        ecoResDistinctProduct.clear();
        ecoResDistinctProduct.initValue();
        ecoResDistinctProduct.ProductType = EcoResProductType::Item;
        ecoResDistinctProduct.DisplayProductNumber = "Alex1";
        ecoResDistinctProduct.SearchName = "Alex1";
        if (ecoResDistinctProduct.validateWrite())
        {
            ecoResDistinctProduct.insert();
            ecoResProductIdentifier.clear();
            ecoResProductIdentifier.initValue();
            ecoResProductIdentifier.ProductNumber = "Alex1";
            ecoResProductIdentifier.Product = ecoResDistinctProduct.RecId;
            ecoResProductIdentifier.insert();
            //Storage dimension group
            ecoResStorageDimensionGroupProduct.clear();
            ecoResStorageDimensionGroupProduct.initValue();
            ecoResStorageDimensionGroupProduct.Product = ecoResDistinctProduct.RecId;
            ecoResStorageDimensionGroupProduct.StorageDimensionGroup = EcoResStorageDimensionGroup::findByDimensionGroupName("SW").RecId;
            if (ecoResStorageDimensionGroupProduct.validateWrite())
            {
                ecoResStorageDimensionGroupProduct.insert();
            }
            //Tracking dimension group
            ecoResTrackingDimensionGroupProduct.clear();
            ecoResTrackingDimensionGroupProduct.initValue();
            ecoResTrackingDimensionGroupProduct.Product = ecoResDistinctProduct.RecId;
            ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup = EcoResTrackingDimensionGroup::findByDimensionGroupName("STD").RecId;
            if (ecoResTrackingDimensionGroupProduct.validateWrite())
            {
                ecoResTrackingDimensionGroupProduct.insert();
            }
            EcoResProductTranslation::createOrUpdateTranslation(ecoResDistinctProduct.RecId, "Alex1","Alex1");
            //Released product
            inventTable.clear();
            inventTable.initValue();
            inventTable.initFromEcoResProduct(ecoResDistinctProduct);
            inventTable.ItemId = "Alex1";
            inventTable.NameAlias = "Alex1";
            if (inventTable.validateWrite())
            {
                inventTable.insert();
                //Inventory model group
                inventModelGroupItem.clear();
                inventModelGroupItem.initValue();
                inventModelGroupItem.ItemDataAreaId = inventTable.dataAreaId;
                inventModelGroupItem.ItemId = inventTable.ItemId;
                inventModelGroupItem.ModelGroupId = "FIFO";
                inventModelGroupItem.ModelGroupDataAreaId = curext();
                inventModelGroupItem.insert();
                //Item group
                inventItemGroupItem.clear();
                inventItemGroupItem.initValue();
                inventItemGroupItem.ItemDataAreaId = inventTable.dataAreaId;
                inventItemGroupItem.ItemId = inventTable.ItemId;
                inventItemGroupItem.ItemGroupId = "Parts";
                inventItemGroupItem.ItemGroupDataAreaId = curext();
                inventItemGroupItem.insert();
                //Extended product details – Inventory
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId = inventTable.ItemId;
                inventTableModule.ModuleType = ModuleInventPurchSales::Invent;
                inventTableModule.insert();
                //Extended product details – Purchase
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId = inventTable.ItemId;
                inventTableModule.ModuleType = ModuleInventPurchSales::Purch;
                inventTableModule.insert();
                //Extended product details – Sales
                inventTableModule.clear();
                inventTableModule.initValue();
                inventTableModule.ItemId = inventTable.ItemId;
                inventTableModule.ModuleType = ModuleInventPurchSales::Sales;
                inventTableModule.insert();
                //Warehouse items
                InventItemLocation::createDefault(inventTable.ItemId);
                //Supply type setup
                inventItemSetupSupplyType.clear();
                inventItemSetupSupplyType.initValue();
                inventItemSetupSupplyType.ItemId = inventTable.ItemId;
                inventItemSetupSupplyType.ItemDataAreaId = inventTable.DataAreaId;
                inventItemSetupSupplyType.insert();
                //Product storage dimension group
                ecoResStorageDimensionGroupProduct = EcoResStorageDimensionGroupProduct::findByProduct(ecoResDistinctProduct.RecId);
                if (ecoResStorageDimensionGroupProduct.RecId)
                {
                    ecoResStorageDimensionGroupItem.clear();
                    ecoResStorageDimensionGroupItem.initValue();
                    ecoResStorageDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
                    ecoResStorageDimensionGroupItem.ItemId = inventTable.ItemId;
                    ecoResStorageDimensionGroupItem.StorageDimensionGroup   = ecoResStorageDimensionGroupProduct.StorageDimensionGroup;
                    ecoResStorageDimensionGroupItem.insert();
                }
                //Product tracking dimension group
                ecoResTrackingDimensionGroupProduct = EcoResTrackingDimensionGroupProduct::findByProduct(ecoResDistinctProduct.RecId);
                if (ecoResTrackingDimensionGroupProduct.RecId)
                {
                    ecoResTrackingDimensionGroupItem.clear();
                    ecoResTrackingDimensionGroupItem.initValue();
                    ecoResTrackingDimensionGroupItem.ItemDataAreaId = inventTable.DataAreaId;
                    ecoResTrackingDimensionGroupItem.ItemId = inventTable.ItemId;
                    ecoResTrackingDimensionGroupItem.TrackingDimensionGroup = ecoResTrackingDimensionGroupProduct.TrackingDimensionGroup;
                    ecoResTrackingDimensionGroupItem.insert();
                }
            }
        }
        
        
    }
    catch
    {
        error("Error!");
        return;
    }
    info("Done!");
}


*********************************************************
**********************************************************

Release an Item in Specific Company

static void ReleaseProducts(Args _args)
{
EcoResProduct ecoResProduct;
;

select firstOnly ecoResProduct where EcoResProduct.SearchName == "Alex1"; //Audio system
EcoResProductReleaseManagerBase::releaseProduct(ecoResProduct.RecId,CompanyInfo::findDataArea('DAT').RecId);
}


Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...