Building a query Object through Code.
Building a query object:
Query objects in Dynamics 365 for Finance and Operations are used to build SQL
statements for reports, views, forms, and so on. They are normally created in the AOT using
the drag-and-drop functionality and by defining various properties. Query objects can also
be created from the code at runtime. This is normally done when AOT tools cannot handle
complex and/or dynamic queries.
- Build Query in X++: Queries can also be built dynamically with X++ code. Both approaches are used in standard applications. One advantage of making the query dynamics is that it can not be public in the AOT, and is protected against unintentional AOT changes.
- X++ Query Component
- QueryRun
- Query
- QueryBuildDataSource
- QueryBuildFieldList
- QueryBuildRange
- QueryFilter
- QueryBuildDynaLink
- QueryBuildLink
- QueryRun: use query Run objects to execute the query and fetch data.
- Query: The query object is the definition master. its own properties and has one or more data sources added.
- QueryBuildDataSource: using QueryBuildDataSource you add all the tables. You want to join also. This is also where you defined how to result set is to be sorted. The order mode () method lets you define OrderBy() and GroupBy().
- QueryBuildFieldList: The QueryBuildfieldList object defines which field to fetch from the database. The default is a dynamics field list that is equal to a "select*from......" each data source has only one QueryBuildFieldList object which contains information about all selected fields. You also specific aggregate functions like sum, count, and avg with field list object.
- QueryBuildRange: The queryBuildRange object contains a limitation of the query on a single field.
- QueryFilter: The queryFilter object is used to filter the result set of an outer join. It filters the data at a later stage than queryBuildRange objects and filters the parents table based on the child table results.
- QueryBuildLink: Specifies the relation between the two data sources in the join. Can only exist on a child data source.
Step1: Open VS create a new solution, create a runnable class named class CustTableQuery, and enter the following code:
class Lok_CustTableQuery
{
public static void main(Args _args)
{
Query query;
QueryBuildDataSource qbds1;
QueryBuildDataSource qbds2;
QueryBuildRange qbr1;
QueryBuildRange qbr2;
QueryRun queryRun;
CustTable custTable;
query = new Query();
qbds1 = query.addDataSource(tableNum(CustTable));
qbds1.addSortField(
fieldNum(CustTable, AccountNum),
SortOrder::Ascending);
qbr1 = qbds1.addRange(fieldNum(CustTable, AccountStatement));
qbr1.value(queryValue(CustAccountStatement::Yearly));
qbr2 = qbds1.addRange(fieldNum(CustTable, CustGroup));
qbr2.value(
SysQuery::valueLike(queryValue('12345')));
qbds2 = qbds1.addDataSource(tableNum(CustInvoiceTrans));
qbds2.relations(true);
qbds2.joinMode(JoinMode::ExistsJoin);
queryRun = new QueryRun(query);
while (queryRun.next())
{
custTable= queryRun.get(tableNum(CustTable));
info(strFmt("%1, %2, %3", custTable.CustAccount, custTable.CustGroup, custTable.AccountStatement));
}
}
}
First, we create a new query object, Next, we add a new table CustTable data sources to the query objects by calling its addDataSource. Here also call the addSortField method to enable sorting by the Customer Accounts.
Comments
Post a Comment