Tuesday, June 23, 2015

C# - Creating a custom AX AIF web service that accesses logic defined within an AX AOT Class object.


In order to support legacy applications sometimes you may need to pull in data from AX into standalone applications.
Below will show you how to pull in the customer names via a custom class defined within AX but called within C#

Step 1(in AX) Create Class called CustomerInfo.

Step 2(in AX) Add method called getCustomerNameList()  It should contain the following

/// <summary>
/// Get a list of customer names
/// </summary>
/// <returns>
/// List of customer names
/// </returns>
/// <remarks>
/// Used for AIF Intergration with Escalus
/// </remarks>
[SysEntryPointAttribute(true),AifCollectionTypeAttribute('return', Types::String)]
public List getCustomerNameList()
{
    CustTable custTable;
    DirPartyTable  dirPartyTable;
    List customerNames = new List(Types::String);
    //select all customers and names
    while select * from custTable
        join dirPartyTable
        where dirPartyTable.RecId == custTable.Party
    {
        //save the current customer
        customerNames.addEnd(dirPartyTable.Name);
    }
    return customerNames;
}


Step 3(in AX) Create a new service called CustomerInfo. Defined the class as 'CustomerInfo' under the properties for the service.
Then right click on the 'Operations' node on the new service and defined the method (under properties) as getCustomerNameList or whatever you called the method in step 2.


Step 4(in AX) Create a new service group called Customers and include the service CustomerInfo or whatever the service was called in step 3.
Step 5(in C#) Add the following webservice to your project http://<AOSServer>:8101/DynamicsAx/Services/Customers (Customers is the service group created in step 4)


Step 6(in C#) Add the following using statement using <C# Project Class Name>.<WebService Name> example: using AXAIFTest.Customers;


Step 7(in C#) Add the following code to your load statement or a method that can be called via load
            CustomerInfoClient axClient = new CustomerInfoClient();
            CallContext axContext = new CallContext();
           
            //called the method that exists within an AX class object
            string[] customerList = axClient.getCustomerNameList(axContext);
  
            CustomerNames.DataSource = customerList;

As you can see from my previous posts of http://axcalated.blogspot.com/2015/06/c-creating-aif-webservice-to-access-ax.html and http://axcalated.blogspot.com/2014/10/accessing-ax-custtable-dirpatytable.html you can access data and/or logic from within
AX multiple ways. It really just depends on where you want to write the majority your code (C#, AX/X++, or by creating aot objects within AX) and what are the requirements for the client. Personally I would recommend writing a web service to access your data
that way it could not only be implemented within C#/.NET framework languages but any language that supports accessing web services which is really just about any language these days. The only requirement as well for accessing a web service is intranet/internet access
so it leaves a small footprint on the users system along with "planning for the future" as we all know everything is moving to the web including AX 7.

No comments:

Post a Comment