Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

get view from query

(0) ShareShare
ReportReport
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
    320 on at
    RE: get view from query

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

  • Verified answer
    Martin Dráb Profile Picture
    234,027 Most Valuable Professional 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();

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Product updates

Dynamics 365 release plans