Monday, 19 September 2016

How to consume a BAI2 file using AIF

Most credit card companies (American Express, VISA) send statements to their clients in CSV format, so in order to leverage Dynamics AX credit card import functionality developers have to create additional transformation library and plug it into import sequence.
In this article we provide example of such transformation library in C# and step-by-step instructions on how to setup and execute credit card inbound AIF port with Microsoft Dynamics AX 2012, setup Credit cards for employees in Human Resource module and execute the import.
1)      Generate a .dll file based on which format you are sending credit card transactions
2)      Setup Inbound port and Transformation library in AIF
3)      Run Credit card transactions import job using sample file
4)      Check the transactions

Generating .dll code

public class ClassBAI2XML : ITransform
    {
        string axCompany = "USMF";
        string strTransDate, strExchCode, strCardNumber;
        double dblAmount; // for decimals

        List<string> strAmountCurr = new List<string>();
        public void Transform(System.IO.Stream input, System.IO.Stream output, string config)
        {
            StreamReader sreader = new StreamReader(input);
            XmlTextWriter xwriter = new XmlTextWriter(output, Encoding.UTF8);

            string[] dataArray;
            string[] lineValues;

            string lineIn = sreader.ReadLine();

            // Start writing the XML file.
            xwriter.Formatting = Formatting.Indented;
            xwriter.WriteStartDocument();

            xwriter.WriteStartElement("Batch", "http://schemas.microsoft.com/dynamics/2009/06/documents/Batch");

            lineIn = sreader.ReadLine();
            while (lineIn != null)
            {
                dataArray = lineIn.Split('\t');
                lineValues = dataArray[0].Split(',');

                switch (lineValues[0])
                {
                    case "02":
                        strTransDate = lineValues[4];

                        strTransDate = (DateTime.Now.Year.ToString()).Substring(0, 2) + strTransDate.Substring(0, 2) + "-" + strTransDate.Substring(2, 2) + "-" + strTransDate.Substring(4, 2);
                        break;
                    case "03":
                        if (lineValues[2] == "")
                            strExchCode = "AUD";
                        else
                            strExchCode = lineValues[2];

                        strCardNumber = lineValues[1];
                        break;
                    case "16":
                        strAmountCurr.Add(lineValues[2].ToString());
                        break;
                }

                lineIn = sreader.ReadLine();

            }

            for (int i = 0; i <= strAmountCurr.Count - 1; i++)
            {
                dblAmount = double.Parse(strAmountCurr[i]) / 100;  // for decimals

                xwriter.WriteStartElement("Envelope", "http://schemas.microsoft.com/dynamics/2011/01/documents/Message");
                xwriter.WriteStartElement("Header");
                xwriter.WriteElementString("Company", axCompany); // Change the company as appropriate
                xwriter.WriteElementString("Action", "http://schemas.microsoft.com/dynamics/2008/01/services/TrvPBSMaindataService/create");
                xwriter.WriteEndElement(); //Header
                xwriter.WriteStartElement("Body");
                xwriter.WriteStartElement("MessageParts");
                xwriter.WriteStartElement("TrvPBSMaindata", "http://schemas.microsoft.com/dynamics/2008/01/documents/TrvPBSMaindata");
                xwriter.WriteStartElement("TrvPBSMaindata");
                xwriter.WriteAttributeString("class", "entity");
                xwriter.WriteElementString("AmountCurr", dblAmount.ToString()); // for decimals
                xwriter.WriteElementString("AmountLocal", dblAmount.ToString()); // for decimals
                xwriter.WriteElementString("CardNumber", strCardNumber);
                xwriter.WriteElementString("CardType", "VISA");
                xwriter.WriteElementString("ExchCode", strExchCode);
                xwriter.WriteElementString("TransDate", strTransDate);
                xwriter.WriteEndElement();  //TrvPBSMaindata"
                xwriter.WriteEndElement(); //"TrvPBSMaindata", "http://schemas.microsoft.com/dynamics/2008/01/documents/TrvPBSMaindata");
                xwriter.WriteEndElement();//"MessageParts"
                xwriter.WriteEndElement();//"Body"
                xwriter.WriteEndElement(); //"Envelope"
            }

            strAmountCurr.Clear();
            sreader.Close();
            xwriter.WriteEndElement(); // Batch
            xwriter.Close();
        }
    }

BAI2 File format


Adding a New Transform Class to the Assembly
1.  If you add a new transform class to an assembly and you want to use that new transform in the AIF pipeline, you must re-import the assembly as shown in the following steps.
2.      Copy the DLL to the server bin directory. This directory varies depending on your installation, but it may be named something like C:\Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\Bin
3.      Copy the DLL to the client bin directory. This directory varies depending on your installation, but it may be named something like C:\Program Files\Microsoft Dynamics AX\60\Client\Bin
4.      Restart the AOS to load the updated assembly.
5.      Open Microsoft Dynamics AX and type CTRL+SHIFT+W to open a new development work space.
6.      Select Tools > Application Integration Framework > Manage transforms.
7.      In the Manage transforms form, type CTRL+N to add a new transform.
8.      In the Name field, enter a unique name of up to 30 characters.
9.      In the Description field, enter a description of the transform of up to 1,000 characters.
10.  In the Type field, select .NET Assembly.
11.  Click Load, navigate to the assembly that you copied to the client bin directory, and click Open.
12.  In the Class field, select the new class that was added to the assembly.
13.  Type CTRL+S to save the transform. 





Setup inbound port and Transformation library in AIF
1. Create new “NewCreditCardTrans” Inbound port

2. Select “Service Operation”
3. Select “Transform all requests” checkbox
4. Click “Inbound transforms button” to open Inbound transforms form
 

5. Click “Manage transforms” to open Manage transforms form:

6. Click “New” button
7. Click “Load” button, and provide path to transformation DLL generated from the sample project.

8. Fill the rest of the data as shown below, and click “Close”.

9. On Inbound Transforms form, Click “New” button and select transformation created on the previous step.

10. Close the form.
11. Activate Inbound Port by clicking on “Activate” button.

Run Credit card transactions import job
1. Import credit card file by running “Travel and Expense\Periodic\Import from folder” job


2. Choose the directory and inbound port in drop down, Click OK.


3. Close the Infolog.
4. Now you can check the results in “Credit card transactions” form






Hope it helps...!!


No comments:

Post a Comment