Creating View Out of an AOT Query Object

The purpose of today’s featured article is to show how to create automatically view out of a query object in AOT, based on the selected query. This is a very helpful enhancement that provides easy testing with queries.

 

Iteration through AOT elements is done by using the TreeNode class which has very useful methods for finding a specific node, creating and compiling subnodes, retrieving their children and so on.

 

Creating a view and setting some properties through X++ code is showed below. The view is named as the query and by adding the ‘View’ suffix.

 

aot query object 1

 

But there is one problem when it comes to a query that has many data-sources and even some of them have nested data-sources. In order to solve this ‘inception’ problem, we need to iterate through all data-sources and add every field to the view.

 

The following code example shows how to iterate recursively through the data-sources and the addFields function adds the fields to the view.

 

aot query object 2

 

aot query object 3

 

At the end, the newly created view should be compiled and a data dictionary synchronization is recommended:

 

treeNodeView.AOTcompile();
SqlDataDictionary::synchronize();