Building a query object Dynamics 365 finance and operations

Saturday, April 11, 2020

10 STEPS

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.

1. The first step is to open visual studio, if you need to create a solution or project please refer to my earlier I'm blogs. my normal solution and project called D365Tutorial. I Right click the project and select the add a new item menu.

Step 1 image

2. In the menu which pops up when you right click, select the add menu option. In the sub menu which appears click new item.

Step 2 image

3. In the dialog which pops up click the code option on the right hand side. In the list of available items we're going to select Runnable Class (Job). In the name box at the bottom of the screen give the class a name, RunnableClassCustomerQuery.

Step 3 image

4. Now we have out empty class, lets add some pre-prepared text

Step 4 image

5. In this recipe, we will create a query from the code to retrieve customer records from the
. We will select only the customers who have a main currency of USD. First, we create a new query object. Next, we add a new customer data source to the
query object by calling its addDataSource() member method. Next, we add a link to the sales data source to the
query object by calling its addDataSource() member method.
The following two blocks of code create two ranges. The first block of code shows only the
projects of the time & material type and the second one lists only the records where the
currency equals USD. These two filters are automatically added together using
SQL's AND operator. The QueryBuildRange objects are created by calling the addRange()
member method of the QueryBuildDataSource object with the field ID number as the
argument. The range value is set by calling value() on the QueryBuildRange object itself.
We use the queryValue()function from the Global class and the valueLike() function
from the SysQuery class to prepare the values before applying them as a range. More
functions, such as queryNotValue() and queryRange(), can be found in the Global
application class by navigating to AOT | Classes. Note that these functions are actually
shortcuts to the SysQuery application class, which in turn has even more interesting helper
methods that might be handy for every developer.

Step 5 image

6. Now we are ready to run the example. Before we do right click the class RunnableClassCustomerQuery

Step 6 image

7. Click Set as Startup Object

Step 7 image

8. Click start to run the project

Step 8 image

9. review the results

Step 9 image

10. That's it. You're done.

Step 10 image