-->

Modify AOT items from code - AX 2012

maqk® by MolviDSLR Khan | 2:37 PM

The Problem

Hello guys, at times you may require a particular AOT object, like a menu item or a table, or form etc to be modified from code. Consider a task given to you to update the User CAL settings on each  menu item. Now depending on your solution, it can require hundreds of menu items to be modified one by one.

Another example would be to update the configuration key of each AOT object in your solution. In these bulk updates you need to have a means of updating each object and set its specific properties to a particular setting. Such a task was assigned to me, and yes I could have been a labor mule updating each item and going for the other, but I remembered that I am a knowledge worker, that means, I have to use my brain and work smartly with key decisions.

So I realized that there has to be a means of bulk updating each AOT item object and setting its properties to save time and labor :)

So lets take the real time example here. I have been asked to update the User Cal settings of each menu item  and set it to Task. And since I am a smart knowledge worker, I will avoid the very lengthy manual approach and will look for some smart code and already provided interface to modify existing AOT Item.

My case is specific to Menu items, but the code and approach below will work for all objects within the AOT.

TreeNode Class [AX 2012]

As per the very helpful one liner at msdn,
"The TreeNode class is used to get a handle to any node in the AOT. This class also contains methods that are used to maneuver in the tree."

This class can make you do wonders, like adding new objects (all kinds of), compiling them, and much more. This post will be specific to the problem defined in the above paras :)

So now we need to have each menu item in an iterative lop so that we modify them one by one. The question arises, how we do start with this class to modify any existing AOT item. The answer is this class's static FindNode method :)

TreeNode::findNode Method [AX 2012]

One liner from msdn to start with,
"Gets a specified node in the Application Object Tree (AOT)."

Syntax

client server public static TreeNode findNode(str path)

The idea is to pass a starting path to the path parameter. The node the path will point to can work as a parent node as well as sibling node meaning once on a valid node, we can browse both its siblings or pears as well as its sub nodes or child nodes (if it has).

In our eexample, we will give the path of Display menu item to be treated as a  parent node. From there, we will start looping all the sub nodes (display menu items). Consider the following line;

objTreeNode = TreeNode::findNode(@"\Menu Items\Display\");

Now the object objTreeNode is referencing the node shown in the image below;


Now we can loop its sub nodes from the following code:



    TreeNode    objTreeNode;

    objTreeNode = TreeNode::findNode(@"\Menu Items\Display\");

    if (objTreeNode)
    {
        objTreeNode = objTreeNode.AOTfirstChild();
        /*
        change objTreeNode object properties
        */
        while (objTreeNode)
        {
            info(objTreeNode.treeNodeName());
            objTreeNode = objTreeNode.AOTnextSibling();
        }
    }

The above will print in the info log each display menu item one by one.

Updating the properties of an AOT Item

The TreeNode class's method AOTsetProperties can be used to do this. Syntax is as follows;

public void AOTsetProperties(str properties)

The above example is modified as follows to set the View user license and maintain user liccense to Task for all display menu items found in the AOT under the provided path.

    
    TreeNode    objTreeNode;

    objTreeNode = TreeNode::findNode(@"\Menu Items\Display\");

    if (objTreeNode)
    {
        objTreeNode = objTreeNode.AOTfirstChild();
        /*
        change objTreeNode object properties
        */
        while (objTreeNode)
        {            
            objTreeNode.AOTsetProperties("PROPERTIES\n ViewUserLicense  #" + 'TASK' + "\n  MaintainUserLicense  #" + 'TASK' + "\n ENDPROPERTIES\n");
            objTreeNode.AOTsave();
            objTreeNode = objTreeNode.AOTnextSibling();
        }
    }

Happy AOT coding :)

Links


top