Friday 17 August 2012

Importing Products into AX 2012 - Part 1



Distinct Product Import


Microsoft Dynamics AX is shipped with many standard document services that support common business processes. By using the capabilities that AIF provides, you can also customize existing document services or create your own document services.
We have had several questions on how to import items/products into AX 2012.  Hope this helps!



Inbound port of the Product service

To call the service, you need to expose the service on a new inbound port in Microsoft Dynamics AX.

1. Click System administration >Setup > Services and Application Integration Framework > Inbound ports, and then click New.
2. Give the new port a name, such as Product Data Import Services.
3. Enter a description.
4. Under Service Contract Customizations, click Service Operations. The Select service operations form opens.
5. Select the all the product related operations, move them to the Selected operations pane, and then click Close.
6. In the Inbound ports form, click Activate.

 
You can now access the service externally by using the WSDL URI

Select the service Operations: 




After Selection of the services, activate the Port: 





Set up the Product data import service reference in Microsoft Visual Studio 

1. Open Microsoft Visual Studio®, and create a new project. 
2. Add a new service reference by using the WSDL URI for the product data import service from the Inbound ports form. 
3. Add a Using statement for the service reference. 




You are now ready to start coding against the Distinct Product service.

code to import Distinct products from a .csv file by using a Visual Studio service reference 

This section provides the code that shows the correct order of operations to create a new products in Microsoft Dynamics AX 2012.



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 ProductsImport.ProductDataImportService;
using System.IO;
using System.Text.RegularExpressions;


namespace ProductsImport
{
    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 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;
        internal static int TrackingDimGroup = 23;
       

        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 distinct product //
        //                          //
        static void createDistinctProduct(string[] _product)
        {
            AxdEntity_Product_EcoResDistinctProduct distinctProduct = new AxdEntity_Product_EcoResDistinctProduct()
            {
                DisplayProductNumber = _product[ProdNumber], //"Bulb60W",
                ProductType = (AxdEnum_EcoResProductType)Enum.Parse(typeof(AxdEnum_EcoResProductType), _product[ItemType]),//AxdEnum_EcoResProductType.Item,               
                SearchName = _product[Searchname] //"Bulb60W"

            };

            distinctProduct.Translation = new AxdEntity_Translation[1];
            distinctProduct.Translation[0] = new AxdEntity_Translation()
            {
                LanguageId = "en-us",
                Name = _product[ProdName] //"Transparent Bulb 60W"

            };

            distinctProduct.Identifier = new AxdEntity_Identifier[1];
            distinctProduct.Identifier[0] = new AxdEntity_Identifier()
            {
                ProductNumber = _product[ProdNumber] //"Bulb60W"

            };

            distinctProduct.StorageDimGroup = new AxdEntity_StorageDimGroup[1];
            distinctProduct.StorageDimGroup[0] = new AxdEntity_StorageDimGroup()
            {
                Product = _product[ProdNumber], //"Bulb60W",
                StorageDimensionGroup = _product[StorageDimGroup] //"Std-Dim"

            };

            distinctProduct.TrackingDimGroup = new AxdEntity_TrackingDimGroup[1];
            distinctProduct.TrackingDimGroup[0] = new AxdEntity_TrackingDimGroup()
            {
                Product = _product[ProdNumber], //"Bulb60W",
                TrackingDimensionGroup = _product[TrackingDimGroup] //"Std-Dim"
            };


            AxdEcoResProduct axdProduct = new AxdEcoResProduct()
            {
                Product = new AxdEntity_Product_EcoResProduct[1]
                {
                    distinctProduct
                }
            };

            CallContext ctx = new CallContext();
            EcoResProductServiceClient service = new EcoResProductServiceClient();
            try
            {
                service.create(ctx, axdProduct);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                // System.Console.ReadKey();
            }
        }

       
        //                          //
        ////Release a distinct product //
        //                          //

        private static void releaseDistinctProduct(string[] _product)
        {
            /*if (_product[Dateofprice_inv].ToString() == "")
                datetime;*/


            AxdEntity_InventTable inventTable = new AxdEntity_InventTable()
            {
                ItemId = _product[ProdNumber], //"Bulb60W",
                Product = _product[ProdNumber], //"Bulb60W",
                SalesPriceModelBasic = (AxdEnum_SalesPriceModelBasic)Enum.Parse(typeof(AxdEnum_SalesPriceModelBasic), _product[Baseprice]),//_product[Baseprice],
                SerialNumGroupId = _product[SerialNumGroup],


                Invent = new AxdEntity_Invent[1]
                {
                    new AxdEntity_Invent()
                    {
                        ItemId = _product[ProdNumber], //"Bulb60W",
                        UnitId = _product[InventUnitID]
                        , //"Box"                        
                        PriceUnit = decimal.Parse(_product[Priceunit_inv]),                   
                        PriceDate = DateTime.Parse(_product[Dateofprice_inv])

                    }                    
                },
                Purch = new AxdEntity_Purch[1]
                {
                    new AxdEntity_Purch()
                    {
                        ItemId = _product[ProdNumber], //"Bulb60W",
                        UnitId = _product[PurchUnitID], //"Box"                          
                        TaxItemGroupId = _product[Itemsalestaxgroup_PO],
                        PriceUnit = decimal.Parse(_product[Priceunit_purch]),
                        //if(_product[Dateofprice_purch].ToString() != "")
                        PriceDate = DateTime.Parse(_product[Dateofprice_purch]),
                        EndDisc = (AxdEnum_NoYes)Enum.Parse(typeof(AxdEnum_NoYes), _product[Totaldiscount_purch])
                    }
                },
                Sales = new AxdEntity_Sales[1]
                {
                    new AxdEntity_Sales()
                    {
                        ItemId = _product[ProdNumber], //"Bulb60W",
                        UnitId = _product[SalesUnitID], //"Pcs"                        
                        TaxItemGroupId = _product[Itemsalestaxgroup_sales],
                        PriceUnit = decimal.Parse(_product[Priceunit_sales]),
                        PriceDate = DateTime.Parse(_product[Dateofprice_sales]),
                        EndDisc = (AxdEnum_NoYes)Enum.Parse(typeof(AxdEnum_NoYes), _product[Totaldiscount_sales])
                    }
                }
                ,

                /* Invent = new AxdEntity_Invent[1] { new AxdEntity_Invent() { ItemId = _product[ProdNumber], UnitId = _product[InventUnitID] } },
                 Purch = new AxdEntity_Purch[1] { new AxdEntity_Purch() { ItemId = _product[ProdNumber], UnitId = _product[PurchUnitID] } },
                 Sales = new AxdEntity_Sales[1] { new AxdEntity_Sales() { ItemId = _product[ProdNumber], UnitId = _product[SalesUnitID] } },
                */
                InventModelGroupItem = new AxdEntity_InventModelGroupItem[1]
                {
                    new AxdEntity_InventModelGroupItem()
                    {                       
                        ModelGroupId = _product[InventoryModelGroup]
                    }
                },
                InventItemGroupItem = new AxdEntity_InventItemGroupItem[1]
                {
                    new AxdEntity_InventItemGroupItem()
                    {
                        ItemGroupId = _product[ItemGroup]
                    }
                }
            };


            AxdItem item = new AxdItem()
            {
                InventTable = new AxdEntity_InventTable[1]
                {
                    inventTable
                }
            };
            CallContext ctx = new CallContext()
            {
                Company = "CEU"
            };
            ItemServiceClient itemService = new ItemServiceClient();
            try
            {
                itemService.create(ctx, item);
            }
            catch (Exception e) { System.Console.WriteLine(e.Message); System.Console.ReadKey(); }


        }


        private void DistiunctProducts_Click(object sender, EventArgs e)
        {
            List<string[]> ProdData = ParseCSVFile("E:\\Products.csv", true);
            //Create a Product for each row in the .csv file
            foreach (string[] DistinctProduct in ProdData)
            {

                createDistinctProduct(DistinctProduct);
                releaseDistinctProduct(DistinctProduct);//distinct products
            }
        }
       
    }
}

Sample CSV:



Header Record
ProductNumber DAX2012BOM21
Searchname MS Dynamics AX 2012
ProdName MS Dynamics AX 2012
StorageDimGroup STD-DIM
SerialNumGroup CSIG
InventUnitID GM
PurchUnitID GM
SalesUnitID GM
ProdCompanyID CEU
ItemGroup CDS
ItemType Item
InventoryModelGroup STD
Base price PurchPrice
Item sales tax group(PO)
Item sales tax group(sales )
Price unit(purch) 1
Date of price(purch) 1/1/1900
Total discount(purch) Yes
Price unit(inv) 1
Date of price(inv) 1/1/1900
Price unit(sales) 1
Date of price(sales) 1/1/1900
Total discount(sales) Yes


1 comment:

  1. Hi,

    This is through AIF .. bu when we write code thru Ax job , everything is inserting but it is not coming in front end.. I hope i was missing in ecoresdistinct product.. How could i acomplish this? kevin.bs@gmail.com .. Plz reply

    ReplyDelete