Wednesday, 22 February 2017

Execute Menu Items through code

Hi,
I've seen some people trying to find out how to execute or run Menu Items programatically on the internet.

So I thought I'd best blog about it.

We have three types of Menu Items: Action, Output and Display.

An action is frequently a class that performs some business operation, an output is a report and a display is a form.

Menu Items are represented by the MenuFunction class. Here's some code example to run Menu Items through code:

?
1
2
3
4
MenuFunction menuFunction;
menuFunction = new MenuFunction(menuItemDisplayStr(MyDisplayMenuItem),
MenuItemType::Display);
menuFunction.run();

Of course the code above may be changed to execute different kinds of Menu Items, for example, the code below runs an Output Menu Item:
?
1
2
3
4
MenuFunction menuFunction;
menuFunction = new MenuFunction(menuItemOutputStr(MyOutputMenuItem),
MenuItemType::Output);
menuFunction.run();

And if you need to pass any arguments to the Menu Item you're trying to execute, you can pass it with an Args object. The run method accepts an Args parameter, like this:

?
1
2
3
4
5
6
7
Args args = new Args();
args.record(myArgumentRecord);
args.caller(this);
new MenuFunction(menuItemOutputStr(MyOutputMenuItem),
MenuItemType::Output).run(args);

You should always use functions to get the name of the menu item instead of using a hardcoded string. This guarantees that if someone changes the name of the menu item, your code stops compiling and you have time to fix it before shipping it. The following are the three functions used to get the menu items' name. Their names are pretty straightforward:


Happy DAXing...

No comments:

Post a Comment