Queries in AX D365

Hi,

Queries - A query performs the same function as select statements, more flexibility, especially when sorting and specifying ranges.
Query class provides the framework for the query
QueryRun class starts this framework dynamically
QueryBuildDataSource Class - Data sources are what queries are built upon
QueryBuildRange  - Class defines which records should be selected from the data source.



Example: 
This  query returns all sales orders, from the SalesTable, for customer '4527', sorted by the SalesId.
Query query;
QueryRun queryRun;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
SalesTable SalesTable;
query = new Query();
//this line attaches a table to the qbds data source object
qbds = query.addDataSource(TableNum (SalesTable));
//this line attaches a range to the 'SalesTable' //data
source, the range is the CustAccount
qbr = qbds.addRange(FieldNum (SalesTable,CustAccount));
// The range is set to '4527'
qbr.value ('4527');
// The query will sort by sales id
qbds.addSortField (FieldNum(SalesTable,SalesId));
// The queryRun object is instantiated using the query
queryRun = new QueryRun(query);
// The queryRun object loops through all records returned
while (queryRun.next())
{// The current record is assigned to the salesTable
variable
SalesTable = queryRun.get(tableNum(SalesTable));
print SalesTable.SalesId;
}


Write a job that will produce a list of vendors. query should have a range that limits the list to vendors in vendor group "100", and sort by account number.

Query query;
QueryRun queryRun;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
vendTable vendTable;
query = new Query();
qbds = query.addDataSource(TableNum(VendTable));
qbr = qbds.addRange(FieldNum(VendTable,VendGroup));
qbr.value('100');
qbds.addSortField(FieldNum(VendTable,AccountNum));
queryRun = new QueryRun(query);
if (queryRun.prompt())
{
while (queryRun.next())
{
VendTable = queryRun.get(tableNum(VendTable));
info(VendTable.Name());
}
}

Comments