Item Master ( Product Subtype---Product) import from CSV file using X++ Programming
In AX2012 we have to create the Records for ECOResProduct.
When the productSubtype is Products then we have to use EcoResdistinctProduct and pass the recid to inventTable and AX classes are used to save the records into the Tables.
public class ItemMasterImport extends RunBase
{
CommaIo csvFile;
Filename filename;
DialogField dialogFilename,dialogcheckbox,dialogFromDate,dialogledger;
container readCon;
counter icount,inserted;
TransDate TransactionDate,Fromdate,TransDate,StartDate,enddate,reversedate;
NoYesID NoYes;
str AccountNum,date1;
args args;
EcoResProductVariantDimensionValue EcoResProductVariantDimensionValue;
RefRecId ecoResDistinctProductVariantRecId;
EcoResProductReleaseManagerBase releaseManager;
container productDimensions;
#define.CurrentVersion(2)
#localmacro.CurrentList
filename,
insertIncorrectRecords,
#endmacro
#localmacro.ListVersion1
filename,
insertIncorrectRecords
#endmacro
#File
}
public Object dialog()
{
DialogRunbase dialog = super();
;
dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));
dialogFilename.value(filename);
dialog.filenameLookupFilter(["All files", #AllFiles]);
return dialog;
}
public boolean getFromDialog()
{
filename = dialogFilename.value();return true;
}
static void main(Args args)
{
ItemMasterImport ItemMasterImport;
;
ItemMasterImport = new ItemMasterImport();
if(ItemMasterImport.prompt())
{
itemMasterImport.ItemMasterUpload();
}
}
void ItemMasterUpload()
{
AxInventTable axInventTable;
AxInventItemGroupItem axInventGroupItem;
AxInventModelGroupItem axInventModelGroupItem;
AxInventTableModule AxInventTableModule;
AxInventTableModule axInventTableModulePurch ,axInventTableModuleInvent;
AxEcoResProductTranslation axProductTranslation;
AxInventItemInventSetup axInventItemInventSetup;
AxInventItemPurchSetup axInventItemPurchSetup;
axInventItemSalesSetup axInventItemSalesSetup;
InventItemSetupSupplyType inventItemSetupSupplyType;
EcoResDistinctProduct ecoResDistictProduct;
EcoResProduct EcoResProduct;
EcoResCategory EcoResCategory;
EcoResCategoryHierarchy EcoResCategoryHierarchy;
ItemId itemId;
InventTable inventTable;
InventItemGroupItem inventItemGroupItem;
Dialog d;
DialogField df1;
CommaTextIo file;
container rec;
InventDim inventDim_InventSiteId;
InventDim inventDim_InventLocationId;
strsearchname,nameAlias,unitid,itemGroupId,modelGroupId,storageDimensionGroup,trackingDimensionGroup,CategoryHierarchyName,CategoryName,purchPrice,salesPrice;
;
csvFile = new CommaIO(filename, 'r');
try
{
if (csvFile)
{
ttsbegin;
while (csvFile.status() == IO_Status::OK)
{
readCon = csvFile.read();
icount++;if (readCon)
{
readCon = csvFile.read();// To remove the first record of file : header
itemId = conPeek(readCon,1);
Searchname = conPeek(readCon,2);
NameAlias = conPeek(readCon,3);
ItemGroupId = conPeek(readCon,4);
ModelGroupId = conPeek(readCon,5);
StorageDimensionGroup = conPeek(readCon,6);
TrackingDimensionGroup = conPeek(readCon,7);
UnitID = conPeek(readCon,8);
CategoryHierarchyName = conPeek(readCon,9);
CategoryName = conPeek(readCon,10);
purchPrice = conPeek(readCon,11);
salesPrice = conPeek(readCon,12);
if(!itemId)
continue;
inventTable= InventTable::find(itemId); if(!inventTable)
{
ecoResDistictProduct.clear();
ecoResDistictProduct.initValue();
ecoResDistictProduct.DisplayProductNumber=itemId;
ecoResDistictProduct.ProductType=ecoResProductType::Item; ecoResDistictProduct.SearchName= Searchname;
ecoResDistictProduct.insert();
axInventTable=axInventTable::construct();
axInventTable.parmItemId(itemId);
axInventTable.parmItemType(ItemType::Item);
axInventTable.parmProduct(ecoResDistictProduct.RecId); axInventTable.parmNameAlias(NameAlias);
//InventTableModulePurch
axInventTableModulePurch = AxInventTableModule::construct();
axInventTableModulePurch.axInventTable(axInventTable);
axInventTableModulePurch.parmItemId(itemId);
axInventTableModulePurch.parmModuleType(ModuleInventPurchSales::Purch);
axInventTableModulePurch.parmUnitId(UnitID); axInventTableModulePurch.parmPrice(str2int(purchPrice));
axInventTable.axInventTableModule_Purch(axInventTableModulePurch);
//InventTableModuleInvent
axInventTableModuleInvent = AxInventTableModule::construct();
axInventTableModuleInvent.axInventTable(axInventTable);
axInventTableModuleInvent.parmItemId(itemId);
axInventTableModuleInvent.parmModuleType(ModuleInventPurchSales::Invent);
axInventTableModuleInvent.parmUnitId(UnitID); axInventTableModuleInvent.parmPrice(str2int(salesPrice));
axInventTable.axInventTableModule_Invent(axInventTableModuleInvent);
axInventTable.save();
//InventGroup Item
axInventGroupItem=AxInventItemGroupItem::construct();
axInventGroupItem.parmItemId(itemId);
axInventGroupItem.parmItemGroupId(ItemGroupId);
axInventGroupItem.parmItemGroupDataAreaId(curext());
axInventGroupItem.parmItemDataAreaId(curext());
axInventGroupItem.save();
//InventModelGroupItem
axInventModelGroupItem=AxInventModelGroupItem::construct();
axInventModelGroupItem.parmItemId(itemId);
axInventModelGroupItem.parmModelGroupId(ModelGroupId); axInventModelGroupItem.parmModelGroupDataAreaId(curext());
axInventModelGroupItem.parmItemDataAreaId(curext());
axInventModelGroupItem.save();
axProductTranslation=AxEcoResProductTranslation::construct();
axProductTranslation.parmDescription(searchname);
axProductTranslation.parmName(searchname);
axProductTranslation.parmProduct(ecoResDistictProduct.RecId); axProductTranslation.parmLanguageId("en-us");
axProductTranslation.save();
//Storage and Tracking dimension
inventTable= InventTable::find(itemId,true);
InventTableInventoryDimensionGroups::updateDimensionGroupsForItem( curext(), itemid, EcoResStorageDimensionGroup::findByDimensionGroupName(StorageDimensionGroup).RecId,
EcoResTrackingDimensionGroup::findByDimensionGroupName(TrackingDimensionGroup).RecId,
inventtable.product);
//Product Category
EcoResProduct = EcoResProduct::findByDisplayProductNumber(itemid); EcoResCategoryHierarchy = EcoResCategoryHierarchy::findByName(CategoryHierarchyName); // category hierarchy
EcoResCategory = EcoResCategory::findByName(CategoryName,EcoResCategoryHierarchy.RecId); // category
EcoResProductCategory::insertEcoResProductCategory(EcoResCategory,EcoResProduct);
axInventItemInventSetup = AxInventItemInventSetup::newInventItemInventSetup(InventItemInventSetup::findDefault(itemId, true));
axInventItemInventSetup.axInventTable(axInventTable);
axInventItemInventSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
axInventItemInventSetup.save();
axInventItemInventSetup = AxInventItemInventSetup::construct();
axInventItemInventSetup.axInventTable(axInventTable);
axInventItemInventSetup.parmItemId(itemId);
axInventItemInventSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
axInventItemInventSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
axInventItemInventSetup.save();
//InventItemPurchaseSetup
axInventItemPurchSetup = axInventItemPurchSetup::newInventItemPurchSetup(InventItemPurchSetup::findDefault(itemId, true));
axInventItemPurchSetup.axInventTable(axInventTable);
axInventItemPurchSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
axInventItemPurchSetup.save();
axInventItemPurchSetup = AxInventItemPurchSetup::construct();
axInventItemPurchSetup.axInventTable(axInventTable);
axInventItemPurchSetup.parmItemId(itemId);
axInventItemPurchSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
axInventItemPurchSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
axInventItemPurchSetup.save();
//InventItemSalesSetup
axInventItemSalesSetup = AxInventItemSalesSetup::newInventItemSalesSetup(InventItemSalesSetup::findDefault(itemId, true));
axInventItemSalesSetup.axInventTable(axInventTable);
axInventItemSalesSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
axInventItemSalesSetup.save();
axInventItemSalesSetup = AxInventItemSalesSetup::construct();
axInventItemSalesSetup.axInventTable(axInventTable);
axInventItemSalesSetup.parmItemId(itemId);
axInventItemSalesSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
axInventItemSalesSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
axInventItemSalesSetup.save();
inserted++;
}
}
}
ttsCommit;
}
icount--;//Remove header recount from total record count
}
catch(Exception::Error)
{
info(strFmt("%1 %2",Exception::Error,icount));
}
}
In AX2012 we have to create the Records for ECOResProduct.
When the productSubtype is Products then we have to use EcoResdistinctProduct and pass the recid to inventTable and AX classes are used to save the records into the Tables.
public class ItemMasterImport extends RunBase
{
CommaIo csvFile;
Filename filename;
DialogField dialogFilename,dialogcheckbox,dialogFromDate,dialogledger;
container readCon;
counter icount,inserted;
TransDate TransactionDate,Fromdate,TransDate,StartDate,enddate,reversedate;
NoYesID NoYes;
str AccountNum,date1;
args args;
EcoResProductVariantDimensionValue EcoResProductVariantDimensionValue;
RefRecId ecoResDistinctProductVariantRecId;
EcoResProductReleaseManagerBase releaseManager;
container productDimensions;
#define.CurrentVersion(2)
#localmacro.CurrentList
filename,
insertIncorrectRecords,
#endmacro
#localmacro.ListVersion1
filename,
insertIncorrectRecords
#endmacro
#File
}
public Object dialog()
{
DialogRunbase dialog = super();
;
dialogFilename = dialog.addField(extendedTypeStr(FilenameOpen));
dialogFilename.value(filename);
dialog.filenameLookupFilter(["All files", #AllFiles]);
return dialog;
}
public boolean getFromDialog()
{
filename = dialogFilename.value();return true;
}
static void main(Args args)
{
ItemMasterImport ItemMasterImport;
;
ItemMasterImport = new ItemMasterImport();
if(ItemMasterImport.prompt())
{
itemMasterImport.ItemMasterUpload();
}
}
void ItemMasterUpload()
{
AxInventTable axInventTable;
AxInventItemGroupItem axInventGroupItem;
AxInventModelGroupItem axInventModelGroupItem;
AxInventTableModule AxInventTableModule;
AxInventTableModule axInventTableModulePurch ,axInventTableModuleInvent;
AxEcoResProductTranslation axProductTranslation;
AxInventItemInventSetup axInventItemInventSetup;
AxInventItemPurchSetup axInventItemPurchSetup;
axInventItemSalesSetup axInventItemSalesSetup;
InventItemSetupSupplyType inventItemSetupSupplyType;
EcoResDistinctProduct ecoResDistictProduct;
EcoResProduct EcoResProduct;
EcoResCategory EcoResCategory;
EcoResCategoryHierarchy EcoResCategoryHierarchy;
ItemId itemId;
InventTable inventTable;
InventItemGroupItem inventItemGroupItem;
Dialog d;
DialogField df1;
CommaTextIo file;
container rec;
InventDim inventDim_InventSiteId;
InventDim inventDim_InventLocationId;
strsearchname,nameAlias,unitid,itemGroupId,modelGroupId,storageDimensionGroup,trackingDimensionGroup,CategoryHierarchyName,CategoryName,purchPrice,salesPrice;
;
csvFile = new CommaIO(filename, 'r');
try
{
if (csvFile)
{
ttsbegin;
while (csvFile.status() == IO_Status::OK)
{
readCon = csvFile.read();
icount++;if (readCon)
{
readCon = csvFile.read();// To remove the first record of file : header
itemId = conPeek(readCon,1);
Searchname = conPeek(readCon,2);
NameAlias = conPeek(readCon,3);
ItemGroupId = conPeek(readCon,4);
ModelGroupId = conPeek(readCon,5);
StorageDimensionGroup = conPeek(readCon,6);
TrackingDimensionGroup = conPeek(readCon,7);
UnitID = conPeek(readCon,8);
CategoryHierarchyName = conPeek(readCon,9);
CategoryName = conPeek(readCon,10);
purchPrice = conPeek(readCon,11);
salesPrice = conPeek(readCon,12);
if(!itemId)
continue;
inventTable= InventTable::find(itemId); if(!inventTable)
{
ecoResDistictProduct.clear();
ecoResDistictProduct.initValue();
ecoResDistictProduct.DisplayProductNumber=itemId;
ecoResDistictProduct.ProductType=ecoResProductType::Item; ecoResDistictProduct.SearchName= Searchname;
ecoResDistictProduct.insert();
axInventTable=axInventTable::construct();
axInventTable.parmItemId(itemId);
axInventTable.parmItemType(ItemType::Item);
axInventTable.parmProduct(ecoResDistictProduct.RecId); axInventTable.parmNameAlias(NameAlias);
//InventTableModulePurch
axInventTableModulePurch = AxInventTableModule::construct();
axInventTableModulePurch.axInventTable(axInventTable);
axInventTableModulePurch.parmItemId(itemId);
axInventTableModulePurch.parmModuleType(ModuleInventPurchSales::Purch);
axInventTableModulePurch.parmUnitId(UnitID); axInventTableModulePurch.parmPrice(str2int(purchPrice));
axInventTable.axInventTableModule_Purch(axInventTableModulePurch);
//InventTableModuleInvent
axInventTableModuleInvent = AxInventTableModule::construct();
axInventTableModuleInvent.axInventTable(axInventTable);
axInventTableModuleInvent.parmItemId(itemId);
axInventTableModuleInvent.parmModuleType(ModuleInventPurchSales::Invent);
axInventTableModuleInvent.parmUnitId(UnitID); axInventTableModuleInvent.parmPrice(str2int(salesPrice));
axInventTable.axInventTableModule_Invent(axInventTableModuleInvent);
axInventTable.save();
//InventGroup Item
axInventGroupItem=AxInventItemGroupItem::construct();
axInventGroupItem.parmItemId(itemId);
axInventGroupItem.parmItemGroupId(ItemGroupId);
axInventGroupItem.parmItemGroupDataAreaId(curext());
axInventGroupItem.parmItemDataAreaId(curext());
axInventGroupItem.save();
//InventModelGroupItem
axInventModelGroupItem=AxInventModelGroupItem::construct();
axInventModelGroupItem.parmItemId(itemId);
axInventModelGroupItem.parmModelGroupId(ModelGroupId); axInventModelGroupItem.parmModelGroupDataAreaId(curext());
axInventModelGroupItem.parmItemDataAreaId(curext());
axInventModelGroupItem.save();
axProductTranslation=AxEcoResProductTranslation::construct();
axProductTranslation.parmDescription(searchname);
axProductTranslation.parmName(searchname);
axProductTranslation.parmProduct(ecoResDistictProduct.RecId); axProductTranslation.parmLanguageId("en-us");
axProductTranslation.save();
//Storage and Tracking dimension
inventTable= InventTable::find(itemId,true);
InventTableInventoryDimensionGroups::updateDimensionGroupsForItem( curext(), itemid, EcoResStorageDimensionGroup::findByDimensionGroupName(StorageDimensionGroup).RecId,
EcoResTrackingDimensionGroup::findByDimensionGroupName(TrackingDimensionGroup).RecId,
inventtable.product);
//Product Category
EcoResProduct = EcoResProduct::findByDisplayProductNumber(itemid); EcoResCategoryHierarchy = EcoResCategoryHierarchy::findByName(CategoryHierarchyName); // category hierarchy
EcoResCategory = EcoResCategory::findByName(CategoryName,EcoResCategoryHierarchy.RecId); // category
EcoResProductCategory::insertEcoResProductCategory(EcoResCategory,EcoResProduct);
axInventItemInventSetup = AxInventItemInventSetup::newInventItemInventSetup(InventItemInventSetup::findDefault(itemId, true));
axInventItemInventSetup.axInventTable(axInventTable);
axInventItemInventSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
axInventItemInventSetup.save();
axInventItemInventSetup = AxInventItemInventSetup::construct();
axInventItemInventSetup.axInventTable(axInventTable);
axInventItemInventSetup.parmItemId(itemId);
axInventItemInventSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
axInventItemInventSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
axInventItemInventSetup.save();
//InventItemPurchaseSetup
axInventItemPurchSetup = axInventItemPurchSetup::newInventItemPurchSetup(InventItemPurchSetup::findDefault(itemId, true));
axInventItemPurchSetup.axInventTable(axInventTable);
axInventItemPurchSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
axInventItemPurchSetup.save();
axInventItemPurchSetup = AxInventItemPurchSetup::construct();
axInventItemPurchSetup.axInventTable(axInventTable);
axInventItemPurchSetup.parmItemId(itemId);
axInventItemPurchSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
axInventItemPurchSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
axInventItemPurchSetup.save();
//InventItemSalesSetup
axInventItemSalesSetup = AxInventItemSalesSetup::newInventItemSalesSetup(InventItemSalesSetup::findDefault(itemId, true));
axInventItemSalesSetup.axInventTable(axInventTable);
axInventItemSalesSetup.parmInventDimIdDefault(inventDim_InventSiteId.InventDimId);
axInventItemSalesSetup.save();
axInventItemSalesSetup = AxInventItemSalesSetup::construct();
axInventItemSalesSetup.axInventTable(axInventTable);
axInventItemSalesSetup.parmItemId(itemId);
axInventItemSalesSetup.parmInventDimId(inventDim_InventSiteId.inventDimId);
axInventItemSalesSetup.parmInventDimIdDefault(inventDim_InventLocationId.InventDimId);
axInventItemSalesSetup.save();
inserted++;
}
}
}
ttsCommit;
}
icount--;//Remove header recount from total record count
}
catch(Exception::Error)
{
info(strFmt("%1 %2",Exception::Error,icount));
}
}
No comments:
Post a Comment