Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

get view from query

Posted on by 320

How can I check what TreeNode object I have found?

Related topic: community.dynamics.com/.../get-the-query-from-view

static void GetViewFromQuery(Args _args)
{
#AOT
#define.QueryName(MainQuery)
#define.viewName('mainView')

TreeNode treeNode;
str tmp;
;
tmp = strFmt(#QueryPath + #AOTDelimiter + #ApplicationDataSourcesPath, identifierStr(#QueryName));
treeNode = TreeNode::findNode(tmp);
if (treeNode)
{
treeNode = treeNode.AOTfirstChild(); // Get the first data source -> view (as table)
if (treeNode)
{
tmp = treeNode.treeNodeName();

if (tmp == #viewName)
{
  info(strFmt("View %1 found", tmp));
}
}
}
}

The code above can be used to find the data source offset within a query. And yes it is possible to compare the name of the datasource to a name I already know....but I find that a erroneous way.

My question is how can I check if the found treenode is of type 'view'?

  • botten Profile Picture
    botten 320 on at
    RE: get view from query

    Thanks Mister Dráb. The isView() did it.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 225,549 Super User on at
    RE: get view from query

    First of all, let me add line indention to your code and use Insert > Code. Please do it by yourself next time. I also improved your code a little bit.

    static void GetViewFromQuery(Args _args)
    {
    	#AOT
    	#define.QueryName(MainQuery)
    	#define.viewName('mainView')
    
    	str queryNodePath = strFmt(#QueryPath   #AOTDelimiter   #ApplicationDataSourcesPath, identifierStr(#QueryName));
    	TreeNode treeNode = TreeNode::findNode(queryNodePath);
    	str nodeName;
    	
    	if (treeNode)
    	{
    		treeNode = treeNode.AOTfirstChild(); // Get the first data source -> view (as table)
    		if (treeNode)
    		{
    			nodeName = treeNode.treeNodeName();
    
    			if (nodeName == #viewName)
    			{
    				info(strFmt("View %1 found", nodeName));
    			}
    		}
    	}
    }

    To get what you need, get the table from the Table property of the node. Then you use SysDictTable to find whether this table is actually a view:

    SysDictTable::newName(tableName).isView();

    By the way, I wouldn't use TreeNode at all. Consider this approach instead:

    Query query = new Query(queryStr(mainView));
    QueryBuildDataSource firstDs = query.dataSource(1);
    SysDictTable::newTableId(firstDs.table()).isView();

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,914 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,549 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans