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.
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 and so on… 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.
At the end, the newly created view should be compiled and a data dictionary synchronization is recommended: