how to create product in ax 2012 through code?

Hi every one,

I want to create a product through code in ax 2012?

can any one please provide me code for this, please…

hi nandini,

Try this code.

static void ProductCreate(Args _args)

{

EcoResProductService erProdSvc;

EcoResEcoResProduct EcoResProd;

EcoResEcoResProduct_Product_Master ProdMast;

EcoResEcoResProduct_Translation Translation;

EcoResEcoResProduct_Identifier Identifier;

EcoResEcoResProduct_ProductDimGroup ProdDimGroup;

//Initialize the service object

erProdSvc = EcoResProductService::construct();

EcoResProd = new EcoResEcoResProduct();

ProdMast = new EcoResEcoResProduct_Product_Master();

//ProdDimGroup = new EcoResEcoResProduct_ProductDimGroup();

//Newly created and initialize prodMast

ProdMast.parmDisplayProductNumber(“IDB-PM002”);

ProdMast.parmProductType(EcoResProductType::Item);

ProdMast.parmSearchName(“IDB Product Master 1”);

//Create a new translation object:

Translation = ProdMast.createTranslation().addNew();

Translation.parmDescription(“IDB product Master”);

Translation.parmLanguageId(“en-us”);

Translation.parmName(“IDB Product Master 1”);

Identifier = ProdMast.createIdentifier().addNew();

Identifier.parmProductNumber(“IDB-PM002”);

ProdDimGroup = ProdMast.createProductDimGroup().addNew();

ProdDimGroup.parmProduct(“IDB-PM001”);

ProdDimGroup.parmProductDimensionGroup(“Col”);

ProdMast.parmVariantConfigurationTechnology(EcoResVariantConfigurationTechnologyType::PredefinedVariants);

EcoResProd.createProduct().add(ProdMast);

erProdSvc.create(EcoResProd);

}

hi PVKrishnaiah,

Thanks for your valuable reply, but this code will create poduct master na… I want to create product only…

How can i do that?

Hi Nandini,

For creating distinct product, you need to use 'EcoResEcoResProduct_Product_Distinct class. Ex below -

//Create new Distinct product
prodDistinct = new EcoResEcoResProduct_Product_Distinct();
prodDistinct.parmDisplayProductNumber();
prodDistinct.parmProductType();
prodDistinct.parmSearchName();

Hi Harish,

Usine ur code I am getting this error “This document does not support the AxEcoResDistinctProduct class.”

Actually Harish I am very much new in AX, I just joined… So can you provide me a proper code please…

Thank you so much in advance…

This was utilizing the AIF, however it should get you started:

//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

//Values have been grabbed, time to call AIF

//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

//CREATE DISTINCT

//////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////

if (productType == “Product” || productType == “Service”) // we have ourselves a distinct product

{

AxdEntity_Product_EcoResDistinctProduct productEntity = new AxdEntity_Product_EcoResDistinctProduct();

productEntity.DisplayProductNumber = productNumber;

productEntity.Identifier = new AxdEntity_Identifier[1];

productEntity.Identifier[0] = new AxdEntity_Identifier();

productEntity.Identifier[0].ProductNumber = productNumber;

if (itemType == “Item”)

{

productEntity.ProductType = AxdEnum_EcoResProductType.Item;

}

else

{

productEntity.ProductType = AxdEnum_EcoResProductType.Service;

}

productEntity.Translation = new AxdEntity_Translation[1];

productEntity.Translation[0] = new AxdEntity_Translation();

productEntity.Translation[0].LanguageId = “en-us”;

productEntity.Translation[0].Name = productName;

productEntity.SearchName = searchName;

productEntity.StorageDimGroup = new AxdEntity_StorageDimGroup[1]; // 11/29/2011 setting this on the released item

productEntity.StorageDimGroup[0] = new AxdEntity_StorageDimGroup();

productEntity.StorageDimGroup[0].Product = productNumber;

productEntity.StorageDimGroup[0].StorageDimensionGroup = prodStorageDim;

productEntity.TrackingDimGroup = new AxdEntity_TrackingDimGroup[1];

productEntity.TrackingDimGroup[0] = new AxdEntity_TrackingDimGroup();

productEntity.TrackingDimGroup[0].Product = productNumber;

productEntity.TrackingDimGroup[0].TrackingDimensionGroup = batchGrp;

AxdEcoResProduct productDocument = new AxdEcoResProduct();

productDocument.Product = new AxdEntity_Product_EcoResProduct[1];

productDocument.Product[0] = productEntity;

//service call

CallContext ctx = new CallContext();

ctx.Company = “inc”;

ctx.Language = “en-us”;

EcoResProductServiceClient prodServiceClient = new EcoResProductServiceClient();

EntityKey[] distinctKey = null;

if (productDocument == null)

{

MessageBox.Show(“BLAH”);

}

try

{

distinctKey = prodServiceClient.create(ctx, productDocument);

//MessageBox.Show("Successfully created " + searchName);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

MessageBox.Show(productNumber + “Failed Creation.”);

errorList.Add(productNumber + “Failed Creation DISTINCT.”);

}

Hi,

I am using this code for creating product in ax 2012. Product is creating finely but when I am going to release that product for any company on UI level then I am getting this error… "Cannot create a record in Item purchase order settings (InventItemPurchSetup). Dimension No.: AllBlank2. The record already exists."

and the code for creating product is :

static void Createp(Args _args)

{

BOM bom;

CompanyInfo companyinfo;

BOMTable bomtable;

BOMVersion bomversion;

InventDim inventdim;

InventTable inventtable;

tmpBOMinformation tmptable, tmptable1;

BOMConfigRoute bOMConfigRoute;

EcoResProductDisplayProductNumber _itemId;

EcoResProduct ecoResProduct; // Table

EcoResProductType itemType; // Item or Service

EcoResVariantConfigurationTechnologyType productType; // Predefined, constraint-based, rule-based

str _itemName;

EcoResProductService erProdSvc;

EcoResEcoResProduct ecoResProd;

EcoResEcoResProduct_Product_Master prodMast;

EcoResEcoResProduct_Translation translation;

EcoResEcoResProduct_Identifier identifier;

EcoResEcoResProduct_ProductDimGroup prodDimGroup;

//----

EcoResEcoResProduct_StorageDimGroup StorageDimGroup;

EcoResEcoResProduct_TrackingDimGroup TrackingDimGroup;

InventModelGroupItem inventmodelitemgroupitem;

EcoResDistinctProduct distinctProductTbl;

EcoResProductTranslation productTranslationTbl;

EcoResStorageDimensionGroup storageDimensionTbl;

EcoResStorageDimensionGroupProduct storageDimGroupProduct;

EcoResStorageDimensionGroupItem storageDimGroupItem;

EcoResTrackingDimensionGroup trackingDimensionTbl;

EcoResTrackingDimensionGroupProduct trackingDimGroupProduct;

EcoResTrackingDimensionGroupItem trackingDimGroupItem;

_itemId =“abc111”;

_ItemName = “abc111”;

distinctProductTbl.initValue();

distinctProductTbl.DisplayProductNumber = _ItemName;

distinctProductTbl.ProductType = EcoResProductType::Item;

distinctProductTbl.SearchName = _ItemName;

distinctProductTbl.doInsert();

productTranslationTbl.initValue();

productTranslationTbl.LanguageId = “en-us”;

productTranslationTbl.Description = _itemName;

productTranslationTbl.Name = _ItemName;

productTranslationTbl.Product = distinctProductTbl.RecId;

productTranslationTbl.doinsert();

if(storageDimensionTbl.RecId)

{

if(distinctProductTbl.RecId)

{

if(! EcoResStorageDimensionGroupProduct::findByProduct(distinctProductTbl.RecId))

{

storageDimGroupProduct.initValue();

storageDimGroupProduct.Product = distinctProductTbl.RecId;

storageDimGroupProduct.StorageDimensionGroup = storageDimensionTbl.RecId;

storageDimGroupProduct.insert();

}

}

if (! EcoResStorageDimensionGroupItem::findByItem(curext(), _itemId))

{

storageDimGroupItem.initValue();

storageDimGroupItem.ItemId = _itemId;

storageDimGroupItem.StorageDimensionGroup = storageDimensionTbl.RecId;

storageDimGroupItem.ItemDataAreaId = curext();

storageDimGroupItem.insert();

}

}

trackingDimensionTbl = EcoResTrackingDimensionGroup::findByDimensionGroupName(“con-dim”);

if(trackingDimensionTbl.RecId)

{

if(distinctProductTbl.RecId)

{

if(! EcoResTrackingDimensionGroupProduct::findByProduct(distinctProductTbl.RecId))

{

trackingDimGroupProduct.initValue();

trackingDimGroupProduct.Product = distinctProductTbl.RecId;

trackingDimGroupProduct.TrackingDimensionGroup = trackingDimensionTbl.RecId;

trackingDimGroupProduct.insert();

}

}

if (! EcoResTrackingDimensionGroupItem::findByItem(curext(), _itemId))

{

trackingDimGroupItem.initValue();

trackingDimGroupItem.ItemId = _itemId;

trackingDimGroupItem.TrackingDimensionGroup = trackingDimensionTbl.RecId;

trackingDimGroupItem.ItemDataAreaId = curext();

trackingDimGroupItem.insert();

}

}

info(“Item created”);

}

Hi ,

What should be the code for Releasing the product to multiple companies .

What shall be the update product code : to update all other fields except the product name ?

Hi Nandini ,chk the below code it will helpful

public container createProduct(str60 _product,

str60 _productname,

str60 _searchname,

str60 _arabicDesc,

str60 _desc,

str60 _color,

str60 _size,

str60 _style,

str60 _site,

str60 _warehouse,

Str60 _vendorId,

str60 _externalDesc,

str60 _countingGroup,

str60 _defaultReceipt,

str60 _defaultIssue,

str60 _defaultLocation,

str60 _productDimension,

str60 _sizeGrpId,

str60 _styleGrpId,

str60 _approveStatus,

str60 _category)

{

EcoResProductService erProdSvc;

EcoResEcoResProduct EcoResProd;

EcoResEcoResProduct_Product_Master ProdMast;

EcoResEcoResProduct_Translation Translation;

EcoResEcoResProduct_Identifier Identifier;

EcoResEcoResProduct_ProductDimGroup ProdDimGroup;

boolean isVariantExist = false;

InventDim inventDim;

InventDimCombination inventDimCombination;

container productDimensions,displayNumber,ret,sizeColorStyle;

EcoResDistinctProductVariant ecoResDistinctProductVariant;

EcoResProductVariantDimensionValue EcoResProductVariantDimensionValue;

RefRecId ecoResDistinctProductVariantRecId;

EcoResProductReleaseManagerBase releaseManager;

CompanyInfo companyInfo;

EcoResProductTranslation productTranslation;

RecId color,size,style;

Inventtable inventtable;

PdsVendorCheckItem vendorCheckItem;

EcoResCategory ecoResCategory;

RetailAttributesGlobalLookup retailAttributesGlobalLookup;

CreateProducMaster productMaste = new CreateProducMaster();

ttsBegin;

if(!EcoResProduct::findByDisplayProductNumber(_product).RecId)

{

//Initialize the service object

erProdSvc = EcoResProductService::construct();

EcoResProd = new EcoResEcoResProduct();

ProdMast = new EcoResEcoResProduct_Product_Master();

//ProdDimGroup = new EcoResEcoResProduct_ProductDimGroup();

//Newly created and initialize prodMast

ProdMast.parmDisplayProductNumber(_product);

ProdMast.parmProductType(EcoResProductType::Item);

ProdMast.parmSearchName(_searchname);

//Create a new translation object:

Translation = ProdMast.createTranslation().addNew();

Translation.parmDescription(_desc);

Translation.parmLanguageId(‘en-us’);

Translation.parmName(_productname);

Identifier = ProdMast.createIdentifier().addNew();

Identifier.parmProductNumber(_product);

ProdDimGroup = ProdMast.createProductDimGroup().addNew();

ProdDimGroup.parmProduct(_product);

ProdDimGroup.parmProductDimensionGroup(_productDimension);

ProdMast.parmVariantConfigurationTechnology(EcoResVariantConfigurationTechnologyType::PredefinedVariants);

EcoResProd.createProduct().add(ProdMast);

erProdSvc.create(EcoResProd);

}

select * from ecoResCategory where ecoResCategory.Name == _category;

select * from retailAttributesGlobalLookup where retailAttributesGlobalLookup.Category == ecoResCategory.RecId;

sizeColorStyle = productMaste.insertProductDimension(_product,_color,_size,_style,retailAttributesGlobalLookup.VariantColorGroup

,retailAttributesGlobalLookup.VariantSizeGroup,retailAttributesGlobalLookup.VariantStyleGroup);

color = conPeek(sizeColorStyle,1);

size = conPeek(sizeColorStyle,2);

style = conPeek(sizeColorStyle,3);

if(!color)

_color = ‘’;

if(!size)

_size = ‘’;

if(!style)

_style =’’;

productDimensions = EcoResProductVariantDimValue::getDimensionValuesContainer(’’,_size,_color,_style);

//if(EcoResProductVariantManager::checkNotExistDistinctProductVariant(EcoResProduct::findByDisplayProductNumber(_product).RecId,

//productDimensions) && color && size && style)

if(!EcoResProductVariantManager::findDistinctProductVariant(EcoResProduct::findByDisplayProductNumber(_product).RecId, productDimensions).RecId && color && size && style)

{

ecoResDistinctProductVariant.DisplayProductNumber = EcoResProductNumberBuilderVariant::buildFromProductNumberAndDimensions(

_product,

productDimensions);

ecoResDistinctProductVariantRecId = EcoResProductVariantManager::createProductVariant(EcoResProduct::findByDisplayProductNumber(_product).RecId

,ecoResDistinctProductVariant.DisplayProductNumber,productDimensions);

}

if(!InventTable::find(_product).RecId)

{

EcoResProductReleaseManagerBase::releaseProduct(EcoResProduct::findByDisplayProductNumber(_product).RecId

,CompanyInfo::findDataArea(curext()).RecId);

}

select inventtable where inventtable.ItemId == _product;

if(inventtable)

{

inventtable.selectForUpdate(true);

inventtable.PdsVendorCheckItem = str2enum(vendorCheckItem,_approveStatus);

inventtable.doUpdate();

}

if(size && color && style)

{

inventDim.InventSizeId = _size;

inventDim.InventColorId = _color;

inventDim.InventStyleId = _style;

isVariantExist = true;

}

inventDim.InventSiteId = _site;

inventDim.InventLocationId = _warehouse;

inventDim = InventDim::findOrCreate(inventDim);

if(!InventDimCombination::find(_product,inventDim.InventDimId).RecId && ecoResDistinctProductVariantRecId)

{

// create InventDimCombination

inventDimCombination.DistinctProductVariant = ecoResDistinctProductVariantRecId;

inventDimCombination.ItemId = _product;

inventDimCombination.InventDimId = inventDim.InventDimId;

inventDimCombination.Insert();

}

ttsCommit;

ret = [inventDim.InventDimId,ecoResDistinctProductVariantRecId,isVariantExist];

return ret;

}