Create Company Item for all items in the company using X++ in AX 2009
to create company item for all items inside the company with all the end points by code using x++
this code will help you :
static void CreateOrDistributeCompanyItem(Args _args)
{
InventTable inventTable;
InventTable inventTableOrg;
InventTableCompany inventTableCompany;
InventTableCompany inventTableInterCompany;
DataArea dataarea;
InterCompanyTmpCreate interCompanyTmpCreate;
CompanyInfo companyInfo;
AifEndpoint aifEndpoint;
LineNum lineNum = 10;
InterCompanyCreate interCompanyCreate;
ItemIdCompany itemIdCompany;
ItemNameCompany itemNameCompany;
ItemId itemId;
InventTable itemtforcomapnies;
InventItemLocation inventItemLocation;
InventTableModule inventTableModule;
boolean exist=false;
str itemids;
;
ttsbegin;
while select forupdate inventTableOrg
{
select firstonly inventTableCompany where inventTableCompany.ItemId == inventTableOrg.ItemId;
if(!inventTableCompany)
{
inventTableCompany.ItemId =inventTableOrg.ItemId;
itemId=inventTableOrg.ItemId;
inventTableCompany.Name =inventTableOrg.itemDescription();
try
{
inventTableCompany.Insert();
}
catch
{}
}
else
{}
inventTableOrg.ItemIdCompany=inventTableOrg.ItemId;
itemtforcomapnies =inventTableOrg;
try
{
inventTableOrg.Update();
}
catch
{}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//get endpoints
while select forupdate aifEndpoint
where aifEndpoint.dataAreaId==companyInfo.company()&& aifEndpoint.InterCompanyOrg==NoYes::Yes
{
changecompany(aifEndpoint.EndpointId)
{
try
{
inventTable=null;
inventItemLocation=null;
inventTableModule=null;
select forupdate inventTable where inventTable.ItemId == itemId;
if(!inventTable)
{
// Master record in InventTable
//select forupdate inventTable;
inventTable.initValue();
// If InventTable has other mandatory fields in addition to
// ItemGroupId and ItemId, they should be defined here
inventTable.ItemGroupId = itemtforcomapnies.ItemGroupId;
inventTable.ItemId = itemId; //itemtforcomapnies.ItemId;
inventTable.ItemName = itemtforcomapnies.ItemName;
inventTable.ItemType = ItemType::Item;
inventTable.ItemIdCompany=inventTable.ItemId;
// You can put default DimGroupId, ModelGroupId, etc. here
try
{
inventTable.insert();
//box::info(inventTable.ItemId + ": Inserted In :"+aifEndpoint.EndpointId);
}
catch
{}
// InventItemLocation for default dimension
select forupdate inventItemLocation;
inventItemLocation.initValue();
inventItemLocation.ItemId = itemId;//itemtforcomapnies.ItemId;
inventItemLocation.InventDimId = InventDim::inventDimIdBlank();
inventItemLocation.insert();
// Three records in InventTableModule (for Cost, Purchase and Sales)
// you can also set the price, unit and other values here
select forupdate inventTableModule;
// Cost
inventTableModule.initValue();
inventTableModule.ItemId = itemtforcomapnies.ItemId;
inventTableModule.ModuleType = ModuleInventPurchSales::Invent;
try
{
inventTableModule.insert();
}
catch
{}
// Purchase order
inventTableModule.initValue();
inventTableModule.ItemId = itemtforcomapnies.ItemId;
inventTableModule.ModuleType = ModuleInventPurchSales::Purch;
try
{
inventTableModule.insert();
}
catch
{}
// Sales order
inventTableModule.initValue();
inventTableModule.ItemId = itemtforcomapnies.ItemId;
inventTableModule.ModuleType = ModuleInventPurchSales::Sales;
try
{
inventTableModule.insert();
}
catch
{}
}
else
{
inventTable.ItemIdCompany=inventTable.ItemId;
// You can put default DimGroupId, ModelGroupId, etc. here
try
{
inventTable.update();
}
catch
{}
}
}
catch
{}
changecompany(companyinfo.company())
{
//box::info(companyinfo.company());
}
}
}
}
ttscommit;
box::info('Finished');
}
Comments