AX 2012: Integrate Dynamics AX with Website

the-web-1

Challenge:

What if you need to integrate a Retail E-Commerce website with the powerful Dynamics AX ERP at backend to leverage numerous functional capabilities it offers in the areas like but not limited to Supply Chain Management. Then you are reading the right blog post! We can achieve it through exposing custom AIF services as web services on IIS using enhanced inbound ports via HTTP adapter. In this post, we’ll expose customers data in Dynamics AX 2012 persisted in CustTable to an external interface using web services.

To simplify things, we’ll create a read service operation and then test our custom AIF service exposed as a web service using .Net console application. Although this blog post uses C# consumer to consume the custom service but the same service can be consumed by any website development platforms like ASP.NET and the possibilities are limitless!

Prerequisites:

1. Full compile revealing no errors.
2. Full CIL revealing no errors.
3. Install web services on IIS.
4. Configure default AIF website created while performing step 3.

Development:

1. Create contract class MAKCustTableContract.

  • Declare class variables for chosen fields of CustTable. Use same EDTs as used by table fields.
  • Create parm methods for each of the class variables.
  • Some of the parm methods are given below for reference.
  • To generate parm methods automatically, read this post.
[DataContractAttribute]
class MAKCustTableContract
{
    CustAccount accountNum;
    CustBankAccountId bankAccount;
    CustCreditMaxMST creditMax;
    CustCreditRating creditRating;
    CustCurrencyCode currency;
    CustGroupId custGroup;
    CustDlvModeId dlvMode;
    DlvReasonId dlvReason;
    CustInvoiceAccount invoiceAccount;
    VendAccount vendAccount;
}
[DataMemberAttribute('AccountNum')]
public CustAccount parmAccountNum(CustAccount _accountNum = accountNum)
{
    accountNum = _accountNum;

    return accountNum;
}

[DataMemberAttribute('BankAccount')]
public CustBankAccountId parmBankAccount(CustBankAccountId _bankAccount = bankAccount)
{
    bankAccount = _bankAccount;

    return bankAccount;
}

3. Create service class MAKCustTableService.

  • Add readCustTable() method.
class MAKCustTableService
{
}
[
    SysEntryPointAttribute(true),
    AifCollectionTypeAttribute('return',Types::Class,classStr(MAKCustTableContract))
]
public List readCustTable()
{
    MAKCustTableContract    contract;
    List                    custlist = new List(Types::Class);
    CustTable               custTable;

    while select custTable
        where custTable.CustGroup == '20'
    {
        contract = new MAKCustTableContract();
        contract.parmAccountNum(custTable.AccountNum);
        contract.parmBankAccount(custTable.BankAccount);
        contract.parmCreditMax(custTable.CreditMax);
        contract.parmCreditRating(custTable.CreditRating);
        contract.parmCurrency(custTable.Currency);
        contract.parmCustGroup(custTable.CustGroup);
        contract.parmDlvMode(custTable.DlvMode);
        contract.parmDlvReason(custTable.DlvReason);
        contract.parmInvoiceAccount(custTable.InvoiceAccount);
        contract.parmVendAccount(custTable.VendAccount);
        
        custlist.addEnd(contract);
    }

    return custList;
}

4. Create new service MAKCustTableService.

  • Create a new service node under AOT > Services.

Untitled

  • Expand service node just created.
  • Right click on Operations to add new service operation and select the class method readCustTable.

Untitled

  • Right click MAKCustTableService > Add-Ins > Register service.
  • Make sure it shows NoError in the status.

Untitled

4. Create enhanced inbound port MAKCustTableServices.

  • Open Functional workspace.
  • Click System administration>Setup>Services and Application Integration Framework>Inbound ports.
  • Click New to create new inbound port.
  • Select HTTP for the adapter.
  • Select default AIF website in the URI.

Untitled

  • Select Service operations from the Service contract customizations fast tab.

Untitled

  • Activate the enhanced inbound port.

Untitled

5. Test the URI address of the enhanced inbound port in the browser to ensure the service is activated.

Untitled

5. Test the AIF custom service with C# consumer in .Net console application.

  • Open Visual Studio 2010.
  • Create new .Net console application project in C#.
  • Add new Service reference in the project Service References.
  • Give the URI of the enhanced inbound port we created in address.

Untitled

  • Give the Program definition as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MAKServiceConsumer
{
    using MAKCustTableServices;

    class Program
    {
        static void Main(string[] args)
        {
            CallContext context;
            MAKCustTableServiceClient client;
            List list;

            context = new CallContext();
            context.Company = "CEU";

            client = new MAKCustTableServiceClient();
            list = client.readCustTable(context).ToList();
        }
    }
}
  • Debug the program to see the web service returning data 🙂

Untitled

Advertisements

3 thoughts on “AX 2012: Integrate Dynamics AX with Website

  1. Hi. You said “to simplify things”, buy you did the most complicated way. There’s no reason to create a custom service to expose a few CustTable fields. Too much code and a lot of failure points in your example. You should use document service – it’s not only easier and quicker in this case, but also strongly recommended and the right way to do it.

    1. Hi Fabio,

      The purpose is NOT to expose CustTable fields only. Instead, to demonstrate how we can expose any custom BUSINESS LOGIC to an external interface. On the other hand, document services are built to exchange standard or custom documents DATA only. For example, with custom services we can:

      1. Initiate a Batch process from external interface, may be an Android device consuming web services.

      2. Generate Sales Invoice report, email report to the Customer, all in a non-interactive mode from external interface.

      All this cannot be done with Document services. Do you agree 🙂

      Further, you mentioned a lot of failure points in my example. Can you point out them?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s