Search code examples
dynamics-ax-2012x++

Add existing display menu item to user favorites


Based on this Code I've build the basics of a function to give each user a predefined set of favorites.

However this particular code creates a new AOT element for each favorite of each user. I would like to configure the menu items manually and simply distribute them with the script.

How can I (in X++) get a reference to an existing \Menu Items\Display node and add that to an object of the Menu class?

This is what I have so far:

MyFavorites obj = new MyFavorites();
Menu menuNode;

MenuFunction mf;
TreeNode treeNode;

info("Applying favorites...");

menuNode = obj.getOrCreateRoot();
if (menuNode == null) return;

treeNode = menuNode.AOTfindChild("Administrator");
if (!treeNode)
{
    menuNode.addSubmenu("Administrator");
    treeNode = menuNode.AOTfindChild("Administrator");
    info("Created submenu");
}
else info("Found submenu");

menuNode = treeNode;

// Here I need help. I don't want to recreate all these AOT nodes every time the script is run.
mf = new MenuFunction("Fav_AllUsers",MenuItemType::Display);
mf.AOTsave();

menuNode.addMenuitem(mf);

Solution

  • Here's a quick job I wrote that adds "SalesTable" menu to your favorites.

    Also here's a link that shows how to copy favorites between users

    A thing to note is the table SysPersonalization, which stores a blob of the data so you'll have to use either the object UserMenuList, Menu, or that table to accomplish what you want. This should get you started though.

    static void Job4(Args _args)
    {
        TreeNode                treeNode;
        TreeNode                menuToAdd = TreeNode::findNode(@"\Menu Items\Display\SalesTable");
        TreeNodeIterator        iterator;
        UserMenuList            userMenu;
        Menu                    menuNode;
    
    
        treeNode = infolog.userNode();
        iterator = treeNode.AOTiterator();
        treeNode = iterator.next();
        if (treeNode)
        {
            userMenu = treeNode;
    
            // find 'My Favorites' user menu; 
            treeNode = userMenu.AOTfindChild("@SYS95713");
    
            // Note menuNode is a different object than userMenu
            menuNode = treeNode;
    
            menuNode.addMenuitem(menuToAdd);
        }    
    }