Thursday, October 23, 2014

Accessing AX CustTable & DirPatyTable (Customer Info) via C# via Linq.

In order to support legacy applications sometimes you need to pull in data from AX into standalone applications. Below will show you how to pull in customer id's + names (CustTable + DirPartyTable)
The following examples will show you how to manually loop through that data or tie it to a combo box via Linq.

You can download the C# Visual Studio Project Here: download now!

Step 1. Start new project within visual studio that has access to an aos via the application explorer
Step 2. Adding the following references to the visual studio project

C:\Program Files (x86)\Microsoft Dynamics AX\6.0\Client\Bin\

Microsoft.Dynamics.AX.Framework.Linq.Data.dll
Microsoft.Dynamics.AX.Framework.Linq.Data.Interface.dll
Microsoft.Dynamics.AX.Framework.Linq.Data.ManagedInteropLayer.dll
Microsoft.Dynamics.AX.ManagedInterop.dll


Then on your form/class add the following using classes

using Microsoft.Dynamics.AX.ManagedInterop;
using Microsoft.Dynamics.AX.Framework.Linq.Data;
using System.Linq;

Step 3. Right click on project and add to AOT
Step 4. After step 3 you can start to add objects from the application explorer into the project. Add this time we should add the table CustTable & DirPartyTable

The following code block will show you how to connect to ax, query the tables and loop through the data or tie it to multiple combo boxes. Its good to note that my name space for this project was Win2AX.

       


 
 


public void loadCustomers(Boolean listInTextBox)
{
           

try
{
               

isCustomersLoaded = false;

// Logon to Dynamics AX (usering windows logon info)
Session axSession = new Session();
axSession.Logon(null, null, axServerName, null);

// Create a query provider needed by the Linq Provider
QueryProvider provider = new AXQueryProvider(null);

//Connect to the table proxy's for the CustTable and DirPartTable tables within AX.
QueryCollection<Win2AX.CustTable> customerCollection = new QueryCollection<Win2AX.CustTable>(provider);

QueryCollection<Win2AX.DirPartyTable> partyCollection = new QueryCollection<Win2AX.DirPartyTable>(provider);



 
//select the account num and dirparty name so we can provide an account num + name

//its good to note that when I didnt include a field list the app crashed. I think linq is limited to the amount of data that can be processed

var allCustomers = (from customer in customerCollection
join partyDesc in partyCollection on customer.Party equals partyDesc.RecId
orderby customer.AccountNum ascending
select new { customer.AccountNum, partyDesc.Name, Description = customer.AccountNum + " - " + partyDesc.Name }).ToList();

allCustomers.Insert(0, new { AccountNum = "<Select Customer>", Name = "<Select Customer>", Description = "<Select Customer>" });

if (listInTextBox)
{

CustomerOutput.Clear();
//go through all of the records

foreach (var custTable in allCustomers)
{
//output the current record
CustomerOutput.AppendText(Environment.NewLine + "Customer Account: " + custTable.AccountNum + " Description: " + custTable.Name);
}

}
else if (!listInTextBox)
{
//bind all combo boxes to our linq list

//combination (Customer Id - Name)
CustomerList.DataSource = allCustomers;
CustomerList.DisplayMember = "Description";
CustomerList.ValueMember = "AccountNum";

//Customer Id only
CustomerIdSelection.DataSource = allCustomers;
CustomerIdSelection.DisplayMember = "AccountNum";
CustomerIdSelection.ValueMember = "AccountNum";

//Name Selection / Account Num Value
CustomerNameSelection.DataSource = allCustomers;
CustomerNameSelection.DisplayMember = "Name";
CustomerNameSelection.ValueMember = "AccountNum";

//signal that all combo boxes are loaded so we can handle comboboxes selected index changes correctly

isCustomersLoaded = true;
}

 
//log off ax
axSession.Logoff();

}
catch (Exception ex)
{

CustomerOutput.Clear();
CustomerOutput.AppendText(ex.Message.ToString());

}

No comments:

Post a Comment