Sample union query from AX 2009

Sample union query from AX 2009

Hi friends;

Queries build with the Query classes now supports unions, meaning that you can combine the result from several tables into one result set. The results you want to combine from the different tables must be structured the same way for all tables.

You could for example create a query combining CustTable and VendTable. This would be particularly useful if you need to present for example a lookup form showing both customers and vendors in the same grid. In earlier version you’d have to push customer and vendor data to a temporary table before being able to present the combined data in one grid.

Here is an example on how to build and use a union query from X++:

static void union(Args _args)
    Query                query;
    QueryBuildDataSource qbdsCustTable;
    QueryBuildDataSource qbdsVendTable;
    QueryRun             queryRun;
    CustTable            custVendTable;
    Map                  mapTableBranches = new Map(types::Integer, typeId2Type(typeId(TableId)));
    SysDictTable         dictTable;

    // The map is used to match the UnionBranchID with a table id
    mapTableBranches.insert(1, tableNum(CustTable));
    mapTableBranches.insert(2, tableNum(VendTable));

    query = new Query();

    qbdsCustTable = query.addDataSource(tableNum(CustTable));
    qbdsCustTable.unionType(UnionType::UnionAll); // Include duplicate records
    qbdsCustTable.fields().addField(fieldNum(CustTable, AccountNum));
    qbdsCustTable.fields().addField(fieldNum(CustTable, Name));

    qbdsVendTable = query.addDataSource(tableNum(Vendtable));
    qbdsVendTable.unionType(UnionType::UnionAll); // Include duplicate records
    qbdsVendTable.fields().addField(fieldNum(VendTable, AccountNum));
    qbdsVendTable.fields().addField(fieldNum(VendTable, Name));

    queryRun = new QueryRun(query);

    while ( 
        custVendTable = queryRun.getNo(1);
        dictTable = SysDictTable::newTableId(mapTableBranches.lookup(custVendTable.unionAllBranchId)); 

        info (strFmt("%1 %2 (%3)", custVendTable.AccountNum,

- Harry


  1. Im using above example for getting an union output. I have use salesTable for both querybuilddatasources (with different ranges). Problem is there are some duplicate Sales orders. I want to get a grouped set of sales order. plz help. my example is as follows.

    static void UnionTestJob(Args _args)

    Query query;
    QueryBuildDataSource qbdsSalesTable1;
    QueryBuildDataSource qbdsSalesTable2;
    QueryRun queryRun;
    SalesTable SalesTable;

    query = new Query();
    qbdsSalesTable1 = query.addDataSource(tableNum(SalesTable));
    qbdsSalesTable1.addRange(fieldnum(Salestable, SalesId)).value('SO_0000001');

    qbdsSalesTable2 = query.addDataSource(tableNum(SalesTable));
    qbdsSalesTable2.addRange(fieldnum(Salestable, SalesTaker)).value('Admin');

    queryRun = new QueryRun(query);

    while (
    SalesTable = queryRun.get(tablenum(SalesTable));

    out put :

    1. Hi Prabath,
      try this after adding range in query data source

      qbdsSalesTable1.addGroupByField(fieldnum(Salestable, SalesId));

      It will group ur datasource table by SalesID field.

  2. I'm truly enjoying the design and layout of your website.
    It's a very easy on the eyes which makes it much more enjoyable for me to come
    here and visit more often. Did you hire out a designer to create your theme?
    Outstanding work!

    Feel free to surf to my web blog: Replica Louis Vuitton

  3. wonderful points altogether, you simply gained a brand new reader.

    What would you suggest about your submit that you just
    made a few days in the past? Any sure?

    My webpage ... Beats By Dre Studio


Post a Comment


Popular posts from this blog

Connecting to Databases through X++ PART -I

X++ code for document attachment

Multiple Tables In Query