Create products using the EcoResProductService

I have a question regarding the service for creating products in AX2012. I have to import products from a file, and create them using the service (see example below). I only want to call the create on the service once, and not for each record, my problem is that when i call the ecoResProduct.createProduct().add(product) I get an error that the product key already exist.

Any suggestions ??

I have the following code:

EcoResEcoResProduct ecoResProduct;
EcoResEcoResProduct_Product product;
EcoResProductService ecoresPRoductService;

ecoresPRoductService=EcoResProductService::construct();
ecoResProduct=new EcoResEcoResProduct();

if (d.run())

{

file = new CommaIo(df1.value(), ‘r’);
file.inFieldDelimiter(’;’);

while (file.status() == IO_Status::Ok)
{

readCon = file.read();

//This object intitializing should be iterated for multiple records.

product=new EcoResEcoResProduct_Product_Master();

product.parmDisplayProductNumber(strLRTrim(conPeek(readCon,1)));

product.parmProductType(EcoResProductType::Item);

product.parmSearchName(strLRTrim(conPeek(readCon,2)));

ecoResProduct.createProduct().add(product);

}

ecoresPRoductService.create(ecoResProduct);

}

This solution worked for me - calling the service with multiple items. This example is reading a commasep. file with col1 - ProductNo, col2 - ProductName:

static void ImportProducts(Args _args)

{

EcoResEcoResProduct ecoResEcoResProduct;
EcoResEcoResProduct_Product_Distinct ProdDist;
EcoResEcoResProduct_translation translation;
EcoResEcoResProduct_Identifier identifier;

EcoResEcoResProduct_ProductDimGroup prodDimGroup;
EcoResEcoResProduct_StorageDimGroup storDimGroup;
EcoResEcoResProduct_TrackingDimGroup tracDimGroup;

EcoResProductService ecoresPRoductService;

CommaIo file;

container readCon;

Dialog d;

DialogField df1, df2;

EcoResProductDisplayProductNumber ProductDisplayProductNumber;

// Create a comma separated file as input with col1 = product and col2 = description

;

d = new Dialog(“Import items”);

df1 = d.addField(ExtendedTypeStr(“FilenameOpen”));

ecoresPRoductService=EcoResProductService::construct();

ecoResEcoResProduct= new EcoResEcoResProduct();

if (d.run())

{

file = new CommaIo(df1.value(), ‘r’);

file.inFieldDelimiter(’;’);

while (file.status() == IO_Status::Ok)

{

readCon = file.read();

ProdDist = new EcoResEcoResProduct_Product_Distinct();

ProdDist.parmDisplayProductNumber(strLRTrim(conPeek(readCon,1)));

ProdDist.parmProductType(EcoResProductType::Item);

ProdDist.parmSearchName(strLRTrim(conPeek(readCon,2)));

// Create the translation object

translation = ProdDist.createTranslation().addNew();

translation.parmDescription(strLRTrim(conPeek(readCon,2)));

translation.parmLanguageId(“en-us”);

translation.parmName(strLRTrim(conPeek(readCon,1)));

// Create the identifier object

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

identifier.parmProductNumber(strLRTrim(conPeek(readCon,1)));

// Create the StorageDimgroup object

storDimGroup = ProdDist.createStorageDimGroup().addNew();

storDimGroup.parmProduct(strLRTrim(conPeek(readCon,1)));

storDimGroup.parmStorageDimensionGroup(“Con-Dim”);

// Create the TrackingDimGroup object

tracDimGroup = ProdDist.createTrackingDimGroup().addNew();

tracDimGroup.parmProduct(strLRTrim(conPeek(readCon,1)));

tracDimGroup.parmTrackingDimensionGroup(“Lean-Dim1”);

if(ecoResEcoResProduct.existsProduct())

{

ecoResEcoResProduct.parmProduct().add(ProdDist);

}

else

{

ecoResEcoResProduct.createProduct().add(ProdDist);

}

}

ecoresPRoductService.create(ecoResEcoResProduct);

}

}