Tuesday, June 23, 2015

C# - Creating an AX AIF webservice to access AX CustTable & DirPatyTable (Customer Info) query

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 customer id's + names (CustTable + DirPartyTable) via a webservice from a query that is defined within AX that will allow you to accomplish the same thing as http://axcalated.blogspot.com/2014/10/accessing-ax-custtable-dirpatytable.html
but without requiring the user to install AX as its a webservice so technically you could implament this solution in any language.

Step 1(in AX) Create a query called CustomerInfo. This should contain the table CustTable and sub datasource table DirPartyTable. Only include the fields CustTable.AccountNum & DirPartyTable.Name so that the footprint is as small as can be.
Its a good idea to also add an order by of DirPartyTable.Name so the results are sorted A-Z. But you can sort by number as well.


Step 2(in C#) Add the following webservice to your project http://<AOSServer>:8101/DynamicsAx/Services/QueryService  (by default the port is 8101 but if you changed it during the install you would need to change it in the url as well.
This is a default/built-in webservice provided by AX and should not require you to create anything within AX in order to access it.


Step 3 (in C#) Add the following code

        //this will create a container for the customer number & name
        public class Customer
        {
            public string AccountNum { get; set; }
            public string Name { get; set; }
        }

add the following to the load method or create a new method that can be called from the load method
    


List<Customer> axList = new List<Customer>();
            DataSet dataSet;
            AXQueryService.QueryServiceClient client = new AXQueryService.QueryServiceClient();
            AXQueryService.Paging paging = null;
            //execute a static query defined with the AX AOT
            dataSet = client.ExecuteStaticQuery("CustomerInfo", ref paging);
       
     //go thru all of the results of the query and add them to your customer object list
            for (int custCounter = 0; custCounter <= dataSet.Tables[0].Rows.Count - 1; ++custCounter)
            {
         //get the current row information for the 2 tables
                DataRow custRow = dataSet.Tables["CustTable"].Rows[custCounter];
                DataRow dirPartyTableRow = dataSet.Tables["DirPartyTable.DirPartyTable"].Rows[custCounter];

  //create new customer entry
                Customer axCustomer = new Customer()
                {
                    AccountNum = custRow["AccountNum"].ToString(),
                    Name = dirPartyTableRow["Name"].ToString()
                };
  //add current customer to the overall list
                axList.Add(axCustomer);
            }
           
       
            //tie our newly created customer list to your data source
            CustomerNames.DataSource = axList;
            CustomerNames.DisplayMember = "Name";
            CustomerNames.ValueMember = "AccountNum";
            CustomerIds.DataSource = axList;
            CustomerIds.DisplayMember = "AccountNum";
            CustomerIds.ValueMember = "AccountNum";


That's it. You should now have 2 dropdowns being displayed on the form that are linked to each other.

No comments:

Post a Comment