Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Suggested answer

How to find any node in AOT using name

Posted on by Microsoft Employee

Hello Every One,

Is it possible to find a node under AOT ? (eg: Queries/Data sources/datasource1/datasource2).

As mentioned in the above example Under queries I have Data sources again data sources inside data sources and so on. Now I would like to find data source node using it's name . Either it may be direct node or child or grand child ...

Thanks,
Phani

  • Suggested answer
    Hossein.K Profile Picture
    Hossein.K 6,642 on at
    RE: How to find any node in AOT using name

    Hi

    If you could query the Dynamics AX Model Database to find a string across all objects? Well, using the simple SQL Query below, you can. Here’s how to search X code using Dynamics AX model database:

    SELECT RootHandle.Name    RootElementName,
    
        ElementHandle.Name    ElementName,
    
        ElementTypes.ElementTypeName  Type,
    
        CAST(Sources.SourceText AS nvarchar(MAX)) SourceText
    
    FROM Sources Sources
    
        JOIN ModelElement ElementHandle
    
      ON Sources.SourceHandle     = ElementHandle.ElementHandle
    
        JOIN ModelElement RootHandle
    
      ON RootHandle.ElementHandle = ElementHandle.RootHandle
    
        JOIN ElementTypes ElementTypes
    
      ON ElementTypes.ElementType = ElementHandle.ElementType
    
    WHERE CAST(Sources.SourceText AS nvarchar(MAX)) LIKE '%h.karimi%'

    pastedimage1620719259438v1.png

     
  • Suggested answer
    ergun sahin Profile Picture
    ergun sahin 8,812 Super User on at
    RE: How to find any node in AOT using name

    Thinking about it, there is no need to hesitate the loop as long as we don't print to the screen.
    I've written something more general below. The code is returning all the nodes. I just printed out dataSource and relations but you can see the whole tree if you open the closed info

    static void getNodeNames(Args _args)
    {
        #AOT
        TreeNode    rootObjTreeNode;
        void getNode(TreeNode    _objTreeNode)
        {
            TreeNode    objTreeNode = _objTreeNode;
            TreeNode    objTreeNodeTmp;
            objTreeNode = objTreeNode.AOTfirstChild();
            while (objTreeNode)
            {
                if(objTreeNode.AOTparent() && objTreeNode.AOTparent().treeNodeName() == "Data Sources")
                {
                    info("------DATASOURCENAME------"   objTreeNode.treeNodeName());
                    info(objTreeNode.treeNodePath());
                }
                if(objTreeNode.AOTparent() && objTreeNode.AOTparent().treeNodeName() == "Relations")
                {
                    info("------Relations------"   objTreeNode.treeNodeName());
                }
                //all object info
                //info(objTreeNode.treeNodeName());
                
                if(objTreeNode.AOTfirstChild())//objTreeNode.treeNodeName() == "Data Sources")
                {              
                    getNode(objTreeNode);
                }
                
                objTreeNode = objTreeNode.AOTnextSibling();
                
            }
        }
        ;
        
        //rootObjTreeNode = TreeNode::findNode(#QueriesPath   "\\"   queryStr(custAutoSettlement));
        //rootObjTreeNode = TreeNode::findNode(#FormsPath   "\\"   formStr(CustBillingCode));
        rootObjTreeNode = TreeNode::findNode(#ViewsPath   "\\VendorView" );
        if(rootObjTreeNode)
        {
            getNode(rootObjTreeNode);
        }
    }

  • ergun sahin Profile Picture
    ergun sahin 8,812 Super User on at
    RE: How to find any node in AOT using name

    For Form DataSouce

    static void getFormDataSourceNames(Args _args)
    {
        #AOT
        TreeNode    rootObjTreeNode;
        void getNode(TreeNode    _objTreeNode)
        {
            TreeNode    objTreeNode = _objTreeNode;
            TreeNode    objTreeNodeTmp;
            objTreeNode = objTreeNode.AOTfirstChild();
            while (objTreeNode)
            {
                if(objTreeNode.treeNodeName() == "Data Sources")
                {              
                    getNode(objTreeNode);
                }
                if(objTreeNode.AOTparent() && objTreeNode.AOTparent().treeNodeName() == "Data Sources")
                {
                    info(objTreeNode.treeNodeName());
                }
                objTreeNode = objTreeNode.AOTnextSibling();
            }
        }
        ;
    
        //rootObjTreeNode = TreeNode::findNode(#QueriesPath   "\\"   queryStr(custAutoSettlement));
        rootObjTreeNode = TreeNode::findNode(#FormsPath   "\\"   formStr(CustBillingCode));
        if(rootObjTreeNode)
        {
            getNode(rootObjTreeNode);
        }
    }

  • Suggested answer
    ergun sahin Profile Picture
    ergun sahin 8,812 Super User on at
    RE: How to find any node in AOT using name

    If you want to see all the objects, it is possible to write a common structure that goes around the tree, but the code will visit the nodes that you are not interested in.
    If you want something more targeted, you need to play with codes. For example, below I wrote a code that browses the datasources under Query. It is possible to write similar things.

    static void getQueryDataSourceNames(Args _args)
    {
        #AOT
        TreeNode    rootObjTreeNode;
        void getNode(TreeNode    _objTreeNode)
        {
            TreeNode    objTreeNode = _objTreeNode;
              
            objTreeNode = objTreeNode.AOTfirstChild();
            while (objTreeNode)
            {
                if(objTreeNode.treeNodeName() == "Data Sources")
                {
                    objTreeNode = objTreeNode.AOTfirstChild();
                    if(objTreeNode)
                    {
                        info(objTreeNode.treeNodeName());
                        getNode(objTreeNode);                           
                    }
                    else
                        return;
                }          
                objTreeNode = objTreeNode.AOTnextSibling();
            }   
        }
        ;
    
        rootObjTreeNode = TreeNode::findNode(#QueriesPath   "\\"   queryStr(custAutoSettlement));
       
        if(rootObjTreeNode)
        {
            getNode(rootObjTreeNode);
        }
    }

  • Martin Dráb Profile Picture
    Martin Dráb 225,526 Super User on at
    RE: How to find any node in AOT using name

    Which of the things mentioned above do you mean?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to find any node in AOT using name

    Hi Martin,

    Thanks for you reply,

    Is the above applicable also for other AOT nodes like Queries or is it bit different?

    Thanks,

    Phani

  • Martin Dráb Profile Picture
    Martin Dráb 225,526 Super User on at
    RE: How to find any node in AOT using name

    You have two main options. Either you'll use generic reflections APIs, such as TreeNode (TreeNodeIterator etc.) or SysModel* tables.

    Or, in this particular case, you can use methods of FormRun and related classes. For example:

    FormDataSource formDataSource;
    int i;
    
    for (i = 1; i <= formRun.dataSourceCount(); i  )
    {
    
    	formDataSource = _formRun.dataSource(i);
    	if (formDataSource.name() == xyz)
    	{
    		...
    	}
    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How to find any node in AOT using name

    Hi Ergün Şahin,

    My code should be generic like under Address form there is a data source and again inside one more data source . so just passing a data source name  

    is it possible to find the data source node.it can be present in parent level, child or grand child level. I don't want to point directly as you mentioned above. if it is possible to search be data source name that would help.

    Thanks,

    Phani

  • Suggested answer
    ergun sahin Profile Picture
    ergun sahin 8,812 Super User on at
    RE: How to find any node in AOT using name

    You can use TreeNode::findNode

    TreeNode::findNode("\\Forms\\Address\\Data Sources\\Address\\Methods\\delete");

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,835 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,526 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans