How to use a View to Simplify Working with Query Classes in Dynamics AX

By Chris Roehrich | May 16, 2016

How to use a view to simplify working with query classes in dynamics ax

Have you ever used a View to simplify working with Query classes in Dynamics AX? I have worked on a couple projects where my lookups were not working quite like I wanted them to when working with the Query classes. The problems specifically happened when I wanted to see data from a child table that was two, three or four levels deep. The lookup wouldn’t run or the data coming back into the lookup form would say ‘Unknown’. I thought to myself, it would be nice if the data I wanted to see was in one table and I would not need to do any joins using the Query classes. This is when I realized that using a custom View created in the AOT would be handy.

Let’s say I wanted to see data related to Project Contracts and their Customers that are associated with Projects in my lookup. (Physical table and field names in parentheses):

Project ID (ProjTable.ProjId)
Project Name (ProjTable.Name)
Project Contract ID (ProjTable.ProjInvoiceProjId)
Project Contract Name (ProjInvoiceTable.Description)
Customer ID (ProjTable.CustAccount)
Customer Name (DirpartyTable.Name)


The customer name lives in DirPartyTable so this will require joining three tables to get to it:

ProjTable.CustAccount -> CustTable.AccountNum -> CustTable.Party -> DirPartyTable.RecId

To create the view in the AOT, you can choose a table based one or a query based one. If you want a query based one, then you create the query in the AOT first and then select it using the Properties window of the view object. This is an example of the CustBankAccountCrossCompanyView that is query based:

Chris R_Cust Bank Account


I used a table based query for this solution. The ProjTable will be the parent table and will join the other three tables I listed above. The view looks like this:

AOT Data dictionary views


Now that my view is defined in the AOT, I can now test it in a job or a custom lookup with X++. The following is a job that displays the data for a certain Customer number:

static void ProjectContractInfoViewTest(Args _args)
    ProjectContractInfoView         projectContractInfoView;
    Query                           query;
    QueryRun                        qRun;
    QueryBuildDataSource            qBDSource;
    QueryBuildRange                 querybuildRange;
    CustAccount                     customerID;
    int i = 0;
    customerID = "1010-1010123";    
    query = new Query();
    qBDSource = query.addDataSource(tablenum(ProjectContractInfoView));
    querybuildRange = qBDSource.addRange(fieldnum(ProjectContractInfoView, CustAccount));
    qRun = new QueryRun(query);

    while (
        projectContractInfoView = qRun.get(tablenum(ProjectContractInfoView));
        info("ProjId: " + projectContractInfoView.ProjId + " Contract Id: " + projectContractInfoView.ProjInvoiceProjId 
                + " Contract Desc: " + projectContractInfoView.ProjContractDesc + " Customer Name: " + projectContractInfoView.CustomerName);

    info(int2str(i) + " Records found using range on CustAccount.");

Note: There is only one QueryBuildDataSource required since the view joins the underlying tables behind the scenes.


Here would be a similar example of the job code in a method that would be used in a lookup filtering on a Customer number:

public static void lookupProjectCustomerId(FormControl _formControl, CustAccount _customerID)
    SysTableLookup sysTableLookup =  SysTableLookup::newParameters(tablenum(ProjectContractInfoView), _formControl);
    Query query = new Query();
    QueryBuildDataSource qBDSource;
    QueryBuildRange querybuildRange;

    qBDSource = query.addDataSource(tableNum(ProjectContractInfoView));

    // Filter on the Customer ID
    if (_customerID)
        querybuildRange = qBDSource.addRange(fieldnum(ProjectContractInfoView, CustAccount));

    sysTableLookup.addLookupfield(fieldnum(ProjectContractInfoView, ProjId));
    sysTableLookup.addLookupfield(fieldnum(ProjectContractInfoView, ProjInvoiceProjId));
    sysTableLookup.addLookupfield(fieldnum(ProjectContractInfoView, ProjContractDesc));
    sysTableLookup.addLookupfield(fieldnum(ProjectContractInfoView, CustomerName));
    sysTableLookup.addLookupfield(fieldnum(ProjectContractInfoView, ProjectName));


I hope this technique helps if you run across the same problems joining multiple QueryBuildDataSource objects when you need data from child tables.


Related Posts

Recommended Reading:

Managing Your Business Through Uncertain Times Using Dynamics 365 Finance and Operations

  Dynamics 365 Finance and Operations (F&O) can help you make informed decisions on how to move your business forward. […]

Read the Article
5.13.22 Power Platform

Using Power BI Object Level Security

  The following article will demonstrate how to use Power BI Object Level Security to disable column data based on […]

Read the Article
5.12.22 Dynamics CRM

How to Use the Stoneridge Support Portal

Stoneridge Software’s support portal is an intuitive and useful function that makes it easy for you to access resources to […]

Read the Article
5.6.22 Dynamics GP

Dynamics GP Transaction Removal: Purchase Orders

  Are you having performance issues with Purchase Orders?  Do you find that there are old Purchase Orders on your […]

Read the Article
5.5.22 Dynamics GP

The Real Story about the Long-Term Future of Dynamics GP Support

I’ve seen a number of people put forward comment that Dynamics GP is going away and you have to get […]

Read the Article

New Features in Dynamics 365 Business Central 2022 Wave 1 Release – Financial Enhancements

The Dynamics 365 Businses Central 2022 Wave 1 Release has a lot of new and exciting features to help your […]

Read the Article
4.29.22 Dynamics GP

Dynamics GP Transaction Removals: Bank Reconciliation

  This is part 2 of a 3 part series on Dynamics GP Transaction Removals. These quick tips will hopefully […]

Read the Article
4.28.22 Dynamics GP

Uncommonly Used Features – Integrate Purchasing and Payables to Fixed Assets in Dynamics GP

Being able to integrate Purchasing and Payables to Fixed Assets in Dynamics GP will help you create visibility for your […]

Read the Article

New Features in the Dynamics 365 Business Central 2022 Wave 1 Release – Ease-of-Use Features

  There are many new features in the Dynamics 365 Business Central 2022 Wave 1 Release to get excited about! […]

Read the Article

Start the Conversation

It’s our mission to help clients win. We’d love to talk to you about the right business solutions to help you achieve your goals.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!