Product master and a related Product variant
Continuation of the Importing Products into AX2012 - Part 1, here I am posting the Create product master, dimensions, variants
and release them to the particular company.
Create a product master and a related product
variant
To
create a product master, use the EcoResProductService.create operation. Then
use the EcoResProductMasterDimValue.create operation to associate product
dimension values with the product master. Finally, use the
EcoResProductService.create operation again, this time to create a product
variant.
The code to create a product master is similar
to the code that creates a distinct product. The primary difference is the code
that associates the product master with a product dimension group (Size-Dim is
given in below code).
Release products
A
product must be released to a company before it can be used in that company.
The ItemService and InventDimCombinationService services serve this purpose.
The former can be used to release distinct products and product masters. The
latter can be used to release product variants. A product master must be
released before any of its product variants can be released.
Release product master
The
only information required to release a product to a company is the ID of the
product and the ID by which it will be represented in the company (ItemId). It
is possible to add information to the ItemService service. In the following
example, information about the units used for storage, purchasing, and selling
is provided.
Release a product variant
A
product variant can be released after a related product master has been
released. When you release a product variant to a company, the product variant
can be identified in two different ways:
• Use the product number of the
product variant.
• Use the ID of the associated
product master in the company (ItemId) and the InventDim structure, with the
relevant fields set to the dimension values for the variant (the
ItemId/InventDim approach).
Code:
using System;
using
System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProcutMasterImport.ProductDataImportService;
using System.IO;
using
System.Text.RegularExpressions;
namespace ProcutMasterImport
{
public partial
class Product
: Form
{
internal
static int
ProdNumber = 0;
internal
static int
Searchname = 1;
internal
static int
ProdName = 2;
internal static int StorageDimGroup = 3;
internal
static int
SerialNumGroup = 4;
internal
static int
InventUnitID = 5;
internal
static int
PurchUnitID = 6;
internal
static int
SalesUnitID = 7;
internal
static int ProdCompanyID
= 8;
internal
static int
ItemGroup = 9;
internal
static int
ItemType = 10;
internal
static int
InventoryModelGroup = 11;
internal
static int
Baseprice = 12;
//internal
static int InventWareHouse = 13;
//internal static int PurchWareHouse = 14;
//internal
static int SalesWareHouse = 15;
internal
static int
Itemsalestaxgroup_PO = 13;
internal
static int
Itemsalestaxgroup_sales = 14;
internal
static int
Priceunit_purch = 15;
internal
static int
Dateofprice_purch = 16;
internal
static int
Totaldiscount_purch = 17;
internal
static int
Priceunit_inv = 18;
internal
static int
Dateofprice_inv = 19;
internal
static int
Priceunit_sales = 20;
internal
static int
Dateofprice_sales = 21;
internal
static int
Totaldiscount_sales = 22;
//Master Data
internal
static int
MasterProdNumber = 0;
internal
static int
MasterSearchname = 2;
internal
static int
MasterProdName = 3;
internal
static int
DefaultColor = 5;
internal
static int
DefaultConfig = 6;
internal
static int
DefaultSiz = 7;
internal
static int
BOMUnitID = 9;
internal
static int
InputWeight = 24;
internal
static int
ProductInstanceRelationType = 38;
internal
static int
Tareweight = 47;//Column Number in Excel = AV
internal
static int
TrackingDimGroupName = 49;
internal
static int
MasterStorageDimGroup = 50;
internal
static int
MasterProdDimGroup = 51;
internal
static int
MasterLanguage = 52;
internal
static int
MasterItemGroup = 53;
internal
static int
MasterInventoryModelGroup = 54;
internal
static int
MasterPriceunit_purch = 57;
internal
static int
MasterPriceunit_sales = 58;
internal
static int
MasterPriceunit_inv = 59;
internal
static int
MasterItemsalestaxgroup_PO = 60;
internal
static int
MasterItemsalestaxgroup_sales = 61;
internal
static int
MasterProdCompanyID = 62;
//Dim Import
- Size
internal
static int
MasterProdIDSize = 0;
internal
static int MasterProdSize
= 1;
internal
static int
MasterProdSizeName = 2;
//Dim Import
- Color
internal
static int
MasterProdIDColor = 0;
internal
static int
MasterProdColor = 1;
internal
static int
MasterProdColorName = 2;
//Dim Import
- Config
internal
static int
MasterProdIDConfig = 0;
internal
static int
MasterProdConfig = 1;
internal
static int
MasterProdConfigName = 2;
//VariantaCreation
internal
static int
MasterVariantID = 0;
internal
static int
MasterVariantSearchname = 1;
internal
static int
MasterVariantProdMaster = 2;
internal
static int
MasterVariantName = 3;
internal
static int
MasterVariantConfig = 4;
internal
static int MasterVariantSize
= 5;
internal
static int
MasterVariantColor = 6;
internal
static int
MasterVariantCompanyID = 8;
//internal
static int MasterVariantConfigName = 9;
//internal
static int MasterVariantSizeName = 10;
//internal
static int MasterVariantColorName = 11;
public static List<string[]> ParseCSVFile(string
path, Boolean hasHeaders)
{
List<string[]> CSVRows = new
List<string[]>();
int
rowNumber = 0;
using
(StreamReader inputFile = new StreamReader(path))
{
string
line;
string[]
line1;
while
((line = inputFile.ReadLine()) != null)
{
String
pattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
Regex r = new Regex(pattern);
line1 = r.Split(line);
if
(hasHeaders != true || rowNumber != 0)
{
CSVRows.Add(line1);
}
rowNumber++;
}
}
return
CSVRows;
}
public
Product()
{
InitializeComponent();
}
// //
//Create a
product master and a related product variant //
// //
static void createMaster(string[]
_product)
{
//Boolean
ismastercreated;
//master
definition
AxdEntity_Product_EcoResProductMaster productMaster = new AxdEntity_Product_EcoResProductMaster()
{
DisplayProductNumber =
_product[MasterProdNumber],
ProductType =
AxdEnum_EcoResProductType.Item,
SearchName =
_product[MasterSearchname]
};
productMaster.Translation = new AxdEntity_Translation[1];
productMaster.Translation[0] = new AxdEntity_Translation()
{
LanguageId = "en-us",
Name = _product[MasterProdName]
};
productMaster.Identifier = new AxdEntity_Identifier[1];
productMaster.Identifier[0] = new AxdEntity_Identifier()
{
ProductNumber = _product[MasterProdNumber]
};
productMaster.ProductDimGroup = new AxdEntity_ProductDimGroup[1];
productMaster.ProductDimGroup[0] = new AxdEntity_ProductDimGroup()
{
Product = _product[MasterProdNumber],
ProductDimensionGroup =
_product[MasterProdDimGroup]
};
productMaster.StorageDimGroup = new AxdEntity_StorageDimGroup[1];
productMaster.StorageDimGroup[0] = new AxdEntity_StorageDimGroup()
{
Product =
_product[MasterProdNumber], //"Bulb60W",
StorageDimensionGroup =
_product[MasterStorageDimGroup] //"Std-Dim"
};
productMaster.VariantConfigurationTechnology = AxdEnum_EcoResVariantConfigurationTechnologyType.PredefinedVariants;
AxdEcoResProduct axdProduct = new AxdEcoResProduct()
{
Product = new AxdEntity_Product_EcoResProduct[1]
{
productMaster
}
};
CallContext ctx = new CallContext();
EcoResProductServiceClient
productService = new
EcoResProductServiceClient();
try
{
productService.create(ctx,
axdProduct);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
// //
///master dimensions Size //
// //
static void createMasterDimension_Size(string[] _productDimsSize)
{
//master
dimensions definition (two sizes, L and M)
AxdEntity_MasterDim_EcoResProductMasterSize sizeDimensionL = new AxdEntity_MasterDim_EcoResProductMasterSize()
{
SizeProductMaster =
_productDimsSize[MasterProdIDSize],
Size =
_productDimsSize[MasterProdSize], //"L",
EcoResSize = new AxdEntity_EcoResSize[1]
{
new
AxdEntity_EcoResSize()
{
Name = _productDimsSize[MasterProdSize] //"L"
}
},
Description = _productDimsSize[MasterProdSizeName],
SizeProductDimensionAttribute =
3173
};
AxdEcoResProductMasterDimValue
axdDimValue = new
AxdEcoResProductMasterDimValue()
{
MasterDim = new AxdEntity_MasterDim_EcoResProductMasterDimensionValue[1]
{
sizeDimensionL
}
};
CallContext ctx = new CallContext();
EcoResProductMasterDimValueServiceClient masterDimensionService = new EcoResProductMasterDimValueServiceClient();
try
{
masterDimensionService.create(ctx, axdDimValue);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
// //
///master dimensions CONFIG definition (two sizes, L and M) //
// //
static void createMasterDimension_Config(string[] _productDimsConfig)
{
//master
dimensions definition (two sizes, L and M)
AxdEntity_MasterDim_EcoResProductMasterConfiguration ConfigDimensionL = new AxdEntity_MasterDim_EcoResProductMasterConfiguration()
{
EcoResConf = new AxdEntity_EcoResConf[1]
{
new
AxdEntity_EcoResConf()
{
Name = _productDimsConfig[MasterProdConfig]//_productDimsConfig[MasterProdConfigName]// "L"
}
},
ConfigProductMaster =
_productDimsConfig[MasterProdIDConfig],
Configuration =
_productDimsConfig[MasterProdConfig], //"L",
Description =
_productDimsConfig[MasterProdConfigName],
//AdditionalDescription
= _productDimsConfig[MasterProdConfigName],
ConfigProductDimensionAttribute
= 3170
};
AxdEcoResProductMasterDimValue
axdDimValue = new
AxdEcoResProductMasterDimValue()
{
//SenderId
= "3274",
MasterDim = new
AxdEntity_MasterDim_EcoResProductMasterDimensionValue[1]
{
ConfigDimensionL//, ConfigDimensionM
}
};
CallContext ctx = new CallContext();
EcoResProductMasterDimValueServiceClient masterDimensionService = new EcoResProductMasterDimValueServiceClient();
try
{
masterDimensionService.create(ctx, axdDimValue);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
// //
///master dimensions Size //
// //
static void createMasterDimension_Color(string[] _productDimsColor)
{
//master
dimensions definition (two sizes, L and M)
AxdEntity_MasterDim_EcoResProductMasterColor colorDimensionL = new AxdEntity_MasterDim_EcoResProductMasterColor()
{
ColorProductMaster =
_productDimsColor[MasterProdIDColor],
Color =
_productDimsColor[MasterProdColor], //"L",
EcoResColor = new AxdEntity_EcoResColor[1]
{
new
AxdEntity_EcoResColor()
{
Name = _productDimsColor[MasterProdColor] //"L"
}
},
Description =
_productDimsColor[MasterProdSizeName],
ColorProductDimensionAttribute
= 3169
};
AxdEcoResProductMasterDimValue axdDimValue = new
AxdEcoResProductMasterDimValue()
{
MasterDim = new
AxdEntity_MasterDim_EcoResProductMasterDimensionValue[1]
{
colorDimensionL
}
};
CallContext ctx = new CallContext();
EcoResProductMasterDimValueServiceClient masterDimensionService = new EcoResProductMasterDimValueServiceClient();
try
{
masterDimensionService.create(ctx,
axdDimValue);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
// //
////product
variant definition//
// //
static void createVariant(string[]
_productVariant)
{
//product
variant definition
AxdEntity_Product_EcoResDistinctProductVariant productVariant = new AxdEntity_Product_EcoResDistinctProductVariant()
{
DisplayProductNumber =
_productVariant[MasterVariantID], //"RunningShoeL",
ProductType =
AxdEnum_EcoResProductType.Item,
SearchName =
_productVariant[MasterVariantSearchname], //"RunningShoeL",
ProductMaster =
_productVariant[MasterVariantProdMaster] //"RunningShoe"
};
productVariant.Translation = new AxdEntity_Translation[1];
productVariant.Translation[0] = new AxdEntity_Translation()
{
LanguageId = "en-us",
Name =
_productVariant[MasterVariantName] //"Comfortable
running shoe L size"
};
productVariant.VariantDimValue = new
AxdEntity_VariantDimValue_EcoResProductVariantDimensionValue[3];
if
(_productVariant[MasterVariantSize].ToString() != "")
{
productVariant.VariantDimValue[0] = new AxdEntity_VariantDimValue_EcoResProductVariantSize()
{
DistinctProductVariant =
_productVariant[MasterVariantID], //"RunningShoeL",
ProductDimensionAttribute =
3173,//The ID of the EcoResSize table
Size =
_productVariant[MasterVariantSize], //"L",
EcoResSize = new AxdEntity_EcoResSize1[1]
{
new AxdEntity_EcoResSize1()
{
Name = _productVariant[MasterVariantSize] //"L"
}
}
};
}
//added
by imthiyaz - Color
if
(_productVariant[MasterVariantColor] != "")
{
productVariant.VariantDimValue[1] = new
AxdEntity_VariantDimValue_EcoResProductVariantColor()
{
DistinctProductVariant =
_productVariant[MasterVariantID],
ProductDimensionAttribute =
3169,
Color =
_productVariant[MasterVariantColor], //"L",
EcoResColor = new AxdEntity_EcoResColor1[1]
{
new
AxdEntity_EcoResColor1()
{
Name =
_productVariant[MasterVariantColor]
}
}
};
}
//added
by imthiyaz - Config
if
(_productVariant[MasterVariantConfig] != "")
{
productVariant.VariantDimValue[2]
= new
AxdEntity_VariantDimValue_EcoResProductVariantConfiguration()
{
DistinctProductVariant =
_productVariant[MasterVariantID],
ProductDimensionAttribute =
3170,
Configuration =
_productVariant[MasterVariantConfig], //"L",
EcoResConf = new AxdEntity_EcoResConf1[1]
{
new
AxdEntity_EcoResConf1()
{
Name =
_productVariant[MasterVariantConfig]
}
}
};
}
AxdEcoResProduct axdProduct = new AxdEcoResProduct()
{
Product = new AxdEntity_Product_EcoResProduct[1]
{
productVariant
}
};
CallContext ctx = new CallContext();
EcoResProductServiceClient
productService = new
EcoResProductServiceClient();
try
{
productService.create(ctx,
axdProduct);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
// //
////Release a
product master//
// //
private
static void
releaseMasterProduct(string[] _productRelease)
{
AxdEntity_InventTable inventTable =
new AxdEntity_InventTable()
{
ItemId =
_productRelease[MasterProdNumber], //"Bulb60W",
Product =
_productRelease[MasterProdNumber], //"Bulb60W",
SalesPriceModelBasic =
AxdEnum_SalesPriceModelBasic.PurchPrice, //(AxdEnum_SalesPriceModelBasic)Enum.Parse(typeof(AxdEnum_SalesPriceModelBasic),
_productRelease[Baseprice]),//_product[Baseprice],
//SerialNumGroupId
= _productRelease[SerialNumGroup],
Invent = new AxdEntity_Invent[1]
{
new
AxdEntity_Invent()
{
ItemId =
_productRelease[MasterProdNumber], //"Bulb60W",
UnitId =
_productRelease[MasterPriceunit_inv] //"Box"
}
},
Purch = new AxdEntity_Purch[1]
{
new
AxdEntity_Purch()
{
ItemId =
_productRelease[MasterProdNumber], //"Bulb60W",
UnitId =
_productRelease[MasterPriceunit_purch] //"Box"
}
},
Sales = new AxdEntity_Sales[1]
{
new
AxdEntity_Sales()
{
ItemId =
_productRelease[MasterProdNumber], //"Bulb60W",
UnitId =
_productRelease[MasterPriceunit_sales] //"Pcs"
}
},
InventModelGroupItem = new AxdEntity_InventModelGroupItem[1]
{
new
AxdEntity_InventModelGroupItem()
{
ModelGroupId =
_productRelease[MasterInventoryModelGroup]
}
},
InventItemGroupItem = new AxdEntity_InventItemGroupItem[1]
{
new
AxdEntity_InventItemGroupItem()
{
ItemGroupId =
_productRelease[MasterItemGroup]
}
}
};
/*inventTable.StandardInventColorId
= _productRelease[DefaultColor];
inventTable.StandardConfigId =
_productRelease[DefaultConfig];
inventTable.StandardInventSizeId =
_productRelease[DefaultSiz];*/
//default
size, color, config cvon't be aasuigned here
inventTable.BOMUnitId =
_productRelease[BOMUnitID];
inventTable.NetWeight = Decimal.Parse(_productRelease[InputWeight]);
inventTable.TaraWeightSpecified = true;
inventTable.MetalTypeSpecified = true;
inventTable.TaraWeight = Decimal.Parse(_productRelease[Tareweight]);
AxdItem item = new AxdItem()
{
InventTable = new AxdEntity_InventTable[1]
{
inventTable
}
};
CallContext ctx = new CallContext()
{
Company =
_productRelease[MasterProdCompanyID] //"DMO"
};
ItemServiceClient itemService = new ItemServiceClient();
try
{
itemService.create(ctx, item);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
// //
///Use the ItemId/InventDim approach//
// //
private
static void
releaseProductVariants2(string[]
_productVariantRelease)
{
AxdEntity_InventDimCombination
releasedVariant = new
AxdEntity_InventDimCombination()
{
DistinctProductVariant = "",
ItemId = _productVariantRelease[MasterVariantProdMaster],// "RunningShoe",
InventDimId = "",
InventDim = new AxdEntity_InventDim[1]
{
new
AxdEntity_InventDim()
{
InventSizeId =
_productVariantRelease[MasterVariantSize], //"M",
InventColorId =
_productVariantRelease[MasterVariantColor],
ConfigId =
_productVariantRelease[MasterVariantConfig]
}
}
};
AxdInventDimCombination
inventDimCombination = new
AxdInventDimCombination()
{
InventDimCombination = new AxdEntity_InventDimCombination[1]
{
releasedVariant
}
};
CallContext ctx = new CallContext()
{
Company =
_productVariantRelease[MasterVariantCompanyID] //"DMO"
};
InventDimCombinationServiceClient inventDimCombinationService
= new InventDimCombinationServiceClient();
try
{
inventDimCombinationService.create(ctx, inventDimCombination);
}
catch
(Exception e)
{
System.Console.WriteLine(e.Message);
System.Console.ReadKey();
}
}
private
void ProductMaster_Click(object sender, EventArgs
e)
{
List<string[]> ProdData = ParseCSVFile("D:\\ProductMasters.csv", true);
foreach
(string[] ProdMasterData in ProdData)
{
createMaster(ProdMasterData);
}
}
private
void CreateDims_Click(object
sender, EventArgs e)
{
List<string[]> ProdDims_Size = ParseCSVFile("D:\\MasterDimSize.csv", true);
foreach
(string[] MasterDimensions_Size in ProdDims_Size)
{
createMasterDimension_Size(MasterDimensions_Size);
}
List<string[]> ProdDims_Color = ParseCSVFile("D:\\MasterDimsColor.csv", true);
foreach
(string[] MasterDimensions_Color in ProdDims_Color)
{
createMasterDimension_Color(MasterDimensions_Color);
}
List<string[]> ProdData_Config = ParseCSVFile("D:\\MasterDimConfig.csv", true);
foreach
(string[] MasterDimensions_Config in ProdData_Config)
{
createMasterDimension_Config(MasterDimensions_Config);
}
}
private void
CreateVariants_Click(object sender, EventArgs e)
{
List<string[]> ProdData_Variants = ParseCSVFile("D:\\ProductMasterVariants.csv", true);
foreach
(string[] ProdMasterData_Variants in ProdData_Variants)
{
createVariant(ProdMasterData_Variants);
}
}
private
void ReleaseAll_Click(object
sender, EventArgs e)
{
List<string[]> ProdMasterData_Rel = ParseCSVFile("D:\\ProductMasters.csv", true);
foreach
(string[] ProdMasterDataRelease in ProdMasterData_Rel)
{
releaseMasterProduct(ProdMasterDataRelease);//master
products
}
}
private
void ReleaseVariants_Click_1(object sender, EventArgs
e)
{
List<string[]> ProdData_VariantsRel = ParseCSVFile("D:\\ProductMasterVariants.csv", true);
foreach
(string[] ProdMasterData_VariantsRel in ProdData_VariantsRel)
{
releaseProductVariants2(ProdMasterData_VariantsRel);
}
}
}
}
Sample CSV: