Monday 23 April 2012

BOM Data import in AX 2012





We will demonstrate how to import BOM data by using the BOMDataImport service.


Expose an inbound port for the BOM 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 clickNew.
2. Give the new port a name, such as BOM Services.
3. Enter a description.
4. Under Service Contract Customizations, click Service Operations. The Select service operations form opens.
5. Select the BOMBillofmeterialsservice 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.

Set up the BOM 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 BOM service from the Inbound ports form.
3. Add a Using statement for the service reference.

You are now ready to start coding against the BOM service.

Sample code to import BOM from a .csv file by using a Visual Studio service reference
This section provides a sample that shows the correct order of operations when using the BOM service to create a new BOM 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 BOMService.BOMServiceReference;
using System.IO;
using System.Text.RegularExpressions;

namespace BOMService
{
    public partial class BOMs : Form
    {
        internal static int BOMid = 0;
        internal static int ItemGroup = 1;
        internal static int ItemNumber = 2;
        internal static int Unit = 3;
        internal static int Consumption = 4;
        internal static int LineType = 5;
        internal static int Perseries = 6;
        internal static int Quantity = 7;
        internal static int ResourceConsumption = 8;
        internal static int Active = 10;
        internal static int Approved = 11;
        internal static int Approvedby = 12;
        internal static int Fromdate = 13;
        internal static int Fromqty = 14;
        internal static int BOMName = 15;
        internal static int Todate = 16;
        internal static int CheckBOM = 17;
        internal static int ConfigId = 18;
        internal static int ColorId = 19;
        internal static int SizeId = 20;
        internal static int SiteId = 21;
        internal static int Warehouse = 22;
        internal static int BOMConfig = 23;
        internal static int BOMColor = 24;
        internal static int BOMSize = 25;
        internal static int BOMSite = 26;


        public BOMs()
        {
            InitializeComponent();
        }


        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;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AxdBillsOfMaterials axdBillOfMaterial = new AxdBillsOfMaterials();
            AxdEntity_BOMVersion axdBOMVersion = new AxdEntity_BOMVersion();
            AxdEntity_BOMTable axdBOMTable = new AxdEntity_BOMTable();

            EntityKey[] entityKey;
            CallContext ctx = new CallContext();
            AxdEntity_BOM[] axdBOM = new AxdEntity_BOM[100];

            string BOMID_Current = "";
            string BOMID_Old = "";
            int i = 0;
            Boolean FirstTimeExecution = true;


            //Create a new Instance of the BOM Service client

            BillsofMaterialsServiceClient BOMSvcClient = new BillsofMaterialsServiceClient();
 

            //Read in your .csv file however you want to
            //This is just provided as a reference

            List<string[]> BOMDataAll = ParseCSVFile("D:\\BOMs.csv", true);

            //Create a BOM for each row in the .csv file

           foreach (string[] BOMData in BOMDataAll)
            {
                BOMID_Current = BOMData[BOMid].ToString();
                if (FirstTimeExecution == true)
                {
                    Array.Clear(axdBOM, 0, 100);            

                    axdBOMVersion.BOMId = BOMData[BOMid];
                    axdBOMVersion.ItemId = BOMData[BOMid];
                    axdBOMVersion.ActiveSpecified = true;
                    axdBOMVersion.ApprovedSpecified = true;
                    axdBOMVersion.FromDateSpecified = true;
                    axdBOMVersion.ToDateSpecified = true;
                    axdBOMVersion.FromQtySpecified = true;
                    axdBOMVersion.FromDate = DateTime.Parse(BOMData[Fromdate]);
                    axdBOMVersion.ToDate = DateTime.Parse(BOMData[Todate]);
                    axdBOMVersion.FromQty = Decimal.Parse(BOMData[Fromqty]);
                    axdBOMVersion.Active = (AxdExtType_BOMVersionActive)Enum.Parse(typeof(AxdExtType_BOMVersionActive), BOMData[Active]);

                   axdBOMVersion.Approver = BOMData[Approvedby];
                    // axdBOMVersion.
                    axdBOMVersion.Approved = (AxdExtType_BOMVersionApproved)Enum.Parse(typeof(AxdExtType_BOMVersionApproved), BOMData[Approved]);
                 

                   axdBOMTable = new AxdEntity_BOMTable()
                    {
                        BOMId = BOMData[BOMid],
                        ItemGroupId = BOMData[ItemGroup],
                        SiteId = BOMData[BOMSite],
                        Name = BOMData[BOMName],
                        ApprovedSpecified = true,
                        CheckBOMSpecified = true,
                        CheckBOM = (AxdEnum_NoYes)Enum.Parse(typeof(AxdEnum_NoYes), BOMData[CheckBOM]), 

                        Approved = (AxdEnum_NoYes)Enum.Parse(typeof(AxdEnum_NoYes), BOMData[Approved]),

                        Approver = BOMData[Approvedby]
                    };

                     axdBOMVersion.InventDimId = "00000063_069"; //pass direct dim id
                     BOMID_Old = BOMID_Current;
                    FirstTimeExecution = false;
                }


                if (BOMID_Current.ToString() != BOMID_Old.ToString())

                {

                    axdBOMTable.BOM = new AxdEntity_BOM[i];

                    axdBOMTable.BOM = axdBOM;



                    axdBOMVersion.BOMTable = new AxdEntity_BOMTable[1];

                    axdBOMVersion.BOMTable[0] = axdBOMTable;



                    axdBillOfMaterial.BOMVersion = new AxdEntity_BOMVersion[1];

                    axdBillOfMaterial.BOMVersion[0] = axdBOMVersion;


                    axdBillOfMaterial.DocPurpose = AxdEnum_XMLDocPurpose.Original;

                    axdBillOfMaterial.DocPurposeSpecified = true;


                    try
                    {

                        entityKey = BOMSvcClient.create(ctx, axdBillOfMaterial);

                        i = 0;

                        Array.Clear(axdBOM, 0, 100);

                    }

                    catch (Exception ex)
                   {

                        System.Console.WriteLine(ex.Message);
                    }
 

                    axdBOMVersion.BOMId = BOMData[BOMid];

                    axdBOMVersion.ItemId = BOMData[BOMid];



                    axdBOMVersion.ActiveSpecified = true;

                    axdBOMVersion.ApprovedSpecified = true;

                    axdBOMVersion.FromDateSpecified = true;

                    axdBOMVersion.ToDateSpecified = true;

                    axdBOMVersion.FromQtySpecified = true;



                    axdBOMVersion.FromDate = DateTime.Parse(BOMData[Fromdate]);

                    axdBOMVersion.ToDate = DateTime.Parse(BOMData[Todate]);

                    axdBOMVersion.FromQty = Decimal.Parse(BOMData[Fromqty]);

                    axdBOMVersion.Active = (AxdExtType_BOMVersionActive)Enum.Parse(typeof(AxdExtType_BOMVersionActive), BOMData[Active]);



                    axdBOMVersion.Approver = BOMData[Approvedby];



                    axdBOMVersion.Approved = (AxdExtType_BOMVersionApproved)Enum.Parse(typeof(AxdExtType_BOMVersionApproved), BOMData[Approved]);



                    axdBOMTable = new AxdEntity_BOMTable()

                    {

                        BOMId = BOMData[BOMid],

                        ItemGroupId = BOMData[ItemGroup],

                        SiteId = BOMData[BOMSite],

                        Name = BOMData[BOMName],

                        ApprovedSpecified = true,

                        CheckBOMSpecified = true,

                        CheckBOM = (AxdEnum_NoYes)Enum.Parse(typeof(AxdEnum_NoYes), BOMData[CheckBOM]),



                        Approved = (AxdEnum_NoYes)Enum.Parse(typeof(AxdEnum_NoYes), BOMData[Approved])

                        ,

                        Approver = BOMData[Approvedby]

                    };


                    axdBOMVersion.InventDimId = "00000063_069"; //pass direct dim id

                    BOMID_Old = BOMID_Current;

                }


                if (BOMID_Current.ToString() == BOMID_Old.ToString())

                {
                    axdBOM[i] = new AxdEntity_BOM()

                    {

                        BOMId = BOMData[BOMid],

                        ItemId = BOMData[ItemNumber],

                        BOMType = (AxdEnum_BOMType)Enum.Parse(typeof(AxdEnum_BOMType), BOMData[LineType]),

                        UnitId = BOMData[Unit],

                        BOMQtySpecified = true,

                        BOMQtySerieSpecified = true,

                        BOMConsumpSpecified = true,

                       

                        BOMQty = decimal.Parse(BOMData[Quantity]),

                        WrkCtrConsumption = (AxdExtType_WrkCtrConsumption)Enum.Parse(typeof(AxdExtType_WrkCtrConsumption), BOMData[ResourceConsumption]),

                        BOMQtySerie = decimal.Parse(BOMData[Perseries]),

                        BOMConsump = (AxdEnum_BOMConsumpType)Enum.Parse(typeof(AxdEnum_BOMConsumpType), BOMData[Consumption])

                    };



                    axdBOM[i].InventDimId = "00000063_069"; //pass direct dim id

                    i++;                  

                }

                BOMID_Old = BOMID_Current;
            }
 
            axdBOMVersion.ActiveSpecified = true;
            axdBOMVersion.ApprovedSpecified = true;
            axdBOMVersion.FromDateSpecified = true;
            axdBOMVersion.ToDateSpecified = true;
            axdBOMVersion.FromQtySpecified = true;
            axdBOMTable.BOM = new AxdEntity_BOM[i];
            axdBOMTable.BOM = axdBOM;

            axdBOMVersion.BOMTable = new AxdEntity_BOMTable[1];
            axdBOMVersion.BOMTable[0] = axdBOMTable;

            axdBillOfMaterial.BOMVersion = new AxdEntity_BOMVersion[1];
            axdBillOfMaterial.BOMVersion[0] = axdBOMVersion;

            axdBillOfMaterial.DocPurpose = AxdEnum_XMLDocPurpose.Original;
            axdBillOfMaterial.DocPurposeSpecified = true;
      
            axdBillOfMaterial.DocPurpose = AxdEnum_XMLDocPurpose.Original;                        axdBillOfMaterial.DocPurposeSpecified = true;
           

            try
            {
                entityKey = BOMSvcClient.create(ctx, axdBillOfMaterial);
                MessageBox.Show("Completed Successfully");
            }

            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                MessageBox.Show(ex.Message);
            }

        }

     }
}



Sample .csv file to use for import
The preceding sample assumes that a .csv file with the following headers exists.  

Header Fields
Values row1
Values row2
BOM
7036
7036
ItemGroup
Encl Comp
Encl Comp
Item number
8001
8003
Unit
SqFt
SqFt
Consumption
Constant
Constant
Line type
Item
Item
Per series
0
0
Quantity
5
5
Resource consumption
FALSE
FALSE
Active
Yes
Yes
Approved
Yes
Yes
Approved by
83
83
From date
1/1/1900
1/1/1900
From qty
1
1
BOMName
PRODUCT MODEL NO. 20001
PRODUCT MODEL NO. 20001
To date
1/1/2100
1/1/2100
CheckBOM
Yes
Yes
C


Color


S


Site


Warehouse
11
11
BOMVersionConfig


BOMVersionColor


BOMVersionSize


BOMVersionSite
1
1
BOMVersionDimID


ItemDimID