Axaptapedia is now maintained by AgileCadence For more information please click here.

Query class

From Axaptapedia
Jump to: navigation, search

Dynamics AX Geek used to have a perfect desctiprion of it. [Dynamics AX Geek: Using query()]

Now the page doesn't exist any more, so here's the full text:


As the name implies QueryRun is the executor of a query linked to it. To construct a query you want QueryRun to execute, you need build classes:

  • Query
  • QueryBuildDataSource
  • QueryBuildRange
  • QueryBuildFieldList
  • QueryBuildLink
  • QueryBuildDynaLink

Today’s example will use the first four to demonstrate a query that sums the credit limit field in CustTable grouped by Country and Currency. Additionally a count field indicates how many records are represented in the sum. Ranges are implemented for AccountNum and Country, but the user is allowed to add additional range criteria in the dialog. To demonstrate the status() property I have locked it, so the user can not change it.


QueryRun executes the query. If needed the familiar query dialog can be opened before the query is run. This is done using the prompt() method. SysQueryRun extends QueryRun and has a total of 7 prompt*() methods, with different default behavior. For example promptAllowAddRange() would allow the user to add new where-conditions.


A query represents the select statement. This is where all the strings come together.


Using QueryBuildDataSources you add all the tables you want joined (just one in this example). This is also where you define how the resultset is to be sorted. The orderMode() method lets you define:

  • OrderBy
  • GroupBy


Ranges represent where conditions. Multiple ranges are connected with AND conditions. Unfortunately there is no easy way to change that.


Represents the selected fields of the query. By default all fields are selected. In regular queries you probably wouldn’t use them that often, but when grouping this is the way to define which fields are calculated. SelectionField is an enum with the following values:

  • Avg
  • Count
  • Database
  • Max
  • Min
  • Sum

The result of Test_Query on my test system looks something like this:

<xpp> CA CAD 1120,00 (3 records) CA USD 500,00 (1 records) DE EUR 0,00 (1 records) DK CAD 5000,00 (1 records) DK EUR 300,00 (6 records) DK GBP 560,00 (2 records) ES EUR 0,00 (1 records) IE EUR 0,00 (1 records) NL EUR 20,00 (2 records) NO EUR 3000,00 (1 records) </xpp>

<xpp> static void Test_Query(Args _args) {

   CustTable            custTable;
   Query                query      = new Query();
   QueryRun             qr         = new queryRun(query);
   QueryBuildDataSource qbds       = qr.query().addDataSource(tableNum(CustTable));
   QueryBuildRange      qbrAccN    = qbds.addRange(fieldNum(CustTable,AccountNum));
   QueryBuildRange      qbrCountry = qbds.addRange(fieldNum(CustTable,Country));
   QueryBuildFieldList  qbfl       = qbds.fields();




   if (qr.prompt())
       while (
           custTable = qr.get(tableNum(CustTable));
           print strfmt("%1 %2 %3 (%4 records)",custTable.Country,custTable.Currency,

} </xpp>

See also[edit]

QueryValue function

How to count records in a Query

Expressions in query ranges

Datasource Query Manipulation

Having Clause