In my previous post “Playing with data”, I have made you guys learn how to retrieve data using the select query which you can write anywhere. You just need to declare the table buffer for that. However, there are some cases where it is not recommended to use the select statement to retrieve data for example it is not recommended to use select statement at Form level methods. So, what could be used to retrieve data? The answer is Using Query Object. Normally queries are stored in AOT (see Queries Node in AOT), but they can also
be created from code dynamically. The Query object is used when a user interaction is required, like user wants to add range or sort the data.
In order to create query from code, the objects that needs to be created are,
The query class object defines the structure of the query. Objects of this type are not used for fetching records from the database. Instead, use a QueryRun object that may be assigned a query object.
The QueryBuildDataSource class provides the building blocks that queries are made of. It holds the tables that needed to be added in the query. The hierarchy of the datasources of query are defined in this object. Like if query has a join with another table, so child table is added using the QueryBuildDatasource object of parent table.
The QueryRun class traverses tables in the database while fetching records that satisfy constraints given by the user, and helps gather such constraints from user input.
The object to add the range to the query is,
Now, lets have a code example how to use the objects of these classes. Create a new job and add the following code.
static void CustTableSales(Args _args)
query = new Query();
qbds1 = query.addDataSource(tablenum(CustTable));
qbr1 = qbds1.addRange(fieldnum(CustTable, Blocked));
qbr2 = qbds1.addRange(fieldnum(CustTable, CustGroup));
qbds2 = qbds1.addDataSource(tablenum(SalesTable));
qbds2.addLink(fieldnum(CustTable, AccountNum), fieldnum(SalesTable, CustAccount));
queryRun = new QueryRun(query);
custTable = queryRun.get(tablenum(CustTable));
info(strfmt(“%1 – %2”,custTable.Name,
This job runs the query that selects all active customers who belong to group 10 and have at least one sales order. To set the range value a global function “queryValue(anyType _anyType)” is used which takes a value of any type. There is another static function in the SysQuery class, e.g SysQuery::value().
If the addRange method is called for same field then an OR is added to where clause and if the addRange is called for different field the AND is added to the where clause in the query.
To add OR in the where clause in different field, you can make the expression using the strfmt() global function as,
qbr2.value(strfmt(‘((%1 = “%2”) || (%3 = “%4”))’,
Yes, you can make the expression as you like using the strfmt(), you can use greater than, less than in the query as,
qbr3.value(strFmt(‘(ModifiedDate > %1)’, Date2StrXpp(01012000)));
One thing which should be noted here is the use of Date2StrXpp() global function. It is really a need for a range on date field to use this function. Without this function the query will not be executed correctly and wrong results will be displayed. So, whenever there is a need of applying range on Date field use Date2StrXpp() function.
There are a lot to discuss about query, which cannot be covered in a single post. And it will be boring if I mention everything about query in single post. So wait for new post to explore more about Query Object.
Thanks guys, comments and suggestion will be appreciated.