Query class

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: = Overview = 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
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.

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

QueryBuildDataSource
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

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

QueryBuildFieldList
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:

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)

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; ;   qbrAccN.value('4000..4050'); qbrAccN.status(RangeStatus::Locked); qbrCountry.value('CA..NO'); qbfl.addField(fieldNum(CustTable,CreditMax),SelectionField::Sum); qbfl.addField(fieldnum(CustTable,RecId),SelectionField::Count); qbds.addSortField(fieldnum(CustTable,Country)); qbds.addSortField(fieldNum(CustTable,Currency)); qbds.orderMode(OrderMode::GroupBy); if (qr.prompt) {       while (qr.next) {           custTable = qr.get(tableNum(CustTable)); print strfmt("%1 %2 %3 (%4 records)",custTable.Country,custTable.Currency,                    num2str(custTable.CreditMax,10,2,0,0),custTable.RecId);

}   }    pause; }