Skip to main content

Notifications

Announcements

No record found.

Enable or Disable Visibility of a Menu Item in D365FO using X++

We generally disable or make the control directly invisible in any form using some standard methods like form datasource active() or may be init() or using some methods in interactive classes if using a list page. But let's say we have a requirement to make a menuitem invisible based on certain condition. Then how to achieve it? Here we do not have any form method, right?

So, let's not wait more and check how this can be done.

To achieve the above requirement, we need to subscribe to a standard delegate called checkAddSubMenuDelegate of Class SysMenuNavigationObjectFactory,

Use case here is if the Menu Item should be invisible in some specific legal entity or may be in other condition, we can follow this.

Below is the approach for the same. The condition returns a boolean value which specifies if the menuitem should be visible or invisible.

[SubscribesTo(classstr(SysMenuNavigationObjectFactory), staticdelegatestr(SysMenuNavigationObjectFactory, checkAddSubMenuDelegate))]
    public static void menuItemVisibilityHandler(SysDictMenu _rootMenu, SysDictMenu _subMenu, SysBoxedBoolean _subMenuVisibility)
    {
        if (_subMenu.isMenuItem())   //Checking if it is a menuitem
        {
            var metaElement = _subMenu.GetMenuItemMetaElement();
            if (metaElement != null)
            {
                switch (metaElement.Name)  //Checking the menuitem name and adding condition
                {
                    case menuItemActionStr(MenuItemOne):
                        _subMenuVisibility.value = /<Condition to specify>/;
                        break;
                    case menuItemDisplayStr(MenuItemTwo):
                        _subMenuVisibility.value = /<Condition to specify>/;
                        break;
                }
            }
        }
    }

In this way we can achieve this.


Happy Learning :)

Comments