Adding new feature to context menu for AOT objects in AX 2012

Adding new feature to context menu for AOT objects in AX 2012

Tutorial
Published on
Apr 30, 2015

Sometimes there is a need to add new feature to standard context menu. Let’s assume that we want in a quick way change the developer’s startup project.

Instead of manual changing it in  Tools->Options form, we would like have a possibility to setup it when we right click over the project’s object in Projects window. This could be done in a few steps described below.

Create the class with the feature’s code. To make a solution bit a flexible let’s assume that updating the startup project will be first of several additional features.

Prior to creating class we will create the base enum for menu items: ANG_ContextMenuItem with element UpdateStartupProject:

Now let’s create the class ANG_ContextMenu with method openStartupProject:

class ANG_ContextMenu

{

}

public static ANG_ContextMenu construct()

{

return new ANG_ContextMenu();

}

public static void main(Args _args)

{

ANG_ContextMenu contextMenu = ANG_ContextMenu::construct();

if (_args.parmEnumType() == enumNum(ANG_ContextMenuItem))

{

contextMenu.run(_args);

}

}

public void run(Args _args)

{

switch (_args.parmEnum())

{

case ANG_ContextMenuItem::UpdateStartupProject :

this.updateStartUpProject(_args);

break;

}

}

private void updateStartupProject(Args _args)

{

UserInfo userInfo;

UtilElementName startupProj;

SysContextMenu contextMenu;

TreeNode treeNode;

if (SysContextMenu::startedFrom(_args))

{

contextMenu = _args.parmObject();

treeNode = contextMenu.first();

startupProj = treeNode.treeNodeName();

update_recordSet userInfo

setting StartupProject = startupProj

where userInfo.id == curUserId();

}

}

Create the menu item ANG_UpdateStartupProject for the feature with the properties as is displayed below:

Add the menu item to SysContextMenu menu:

So now it could be visible in context menu:

In the SysContextMenu class we need to modify method verifyItem() to prevent running the feature for other types of AOT objects. We can achieve this by adding below code to the last switch clause in the method:

case menuitemActionStr(ANG_UpdateStartupProject):

if ( firstNode.handle() == classNum(ProjectNode) && //Only launch from project node

match(#pathProjects,firstNode.treeNodePath()))

{

return 1;

}

return 0;

So now we have a new menu item in context menu with possibility to add other usable features in a simple way.

Share it
Written by
Wlodzimierz Wardega