Saturday, March 2, 2013

Building and creating a query in AX using X++ or using an object query (QueryStr function)

Below are two simple X++ queries which either build a query by adding a datasource or by calling a query object. These are pretty basic but I think they could help some people out. I can post more complicated ones later on but I think the simplicity of these two could be helpful without overwhelming someone who might be new to AX.

Create and add datasource and range in X++ code:
// Code using X++ to build the query
Query                   query;
QueryRun                queryRun;
QueryBuildDataSource    qbds;
SalesTable              salesTable;

query    = new Query();
// Add a datasource to the query
qbds     = query.addDataSource(tableNum(SalesTable));
// Add a range to the newly added datasource.
queryRun = new QueryRun(query);
   salesTable  =   queryRun.get(tableNum(SalesTable));
   info(SalesTable.SalesId + ", " + SalesTable.CustAccount);

Use query object in query run functionality:

// Code using a query string
static void Job14(Args _args)
    Query                           query;
    QueryRun                        queryRun;
    QueryBuildDataSource            qbds;
    QueryBuildRange                 qbr;      
    EcoResDistinctProductVariant    ecoResDistinctProductVariant;   
    query = new query(queryStr(EcoResProductVariantsPerCompany));
    queryRun = new QueryRun(query);
    while (
        ecoResDistinctProductVariant = queryRun.get(tableNum(ecoResDistinctProductVariant));
        info (strFmt("%1 - %2", EcoResDistinctProductVariant.SearchName, EcoResDistinctProductVariant.ProductMaster));


  1. Thanks for nice post how if we want to add range using user input from two inter text box (I am new in AX)

  2. Great post, very helpful for beginners like me. Please provide me with more links to similar stuff..Thanks

  3. This comment has been removed by the author.

    1. This comment has been removed by the author.

  4. how to add filter/addRange in query run functionality