Saturday, May 7, 2011

AX 2009: Enhancing your editor scripts

As an AX developer, I find myself using the built-in editor scripts that come with AX a lot. I noticed that there are several times when I would like to have a template for a display method. I create a lot of custom reports that use display methods and  it would save me time if I could build them from a template. The great thing about AX is that if you want something, you can always build it yourself.

I decided today that I was going to go ahead and jump into the Editors Scripts class and build my own display method template. I was surprised at how easy it was. I decided to look at the class that gets called when using the scripts/templates right click option.

It turns out the class being called is named EditorScripts. I took a look at the class and the method "template_method_parm" was the closest to what I was looking for. All this method does is create a dialog to get the parameters used to create the template and passes them to another method.

The parm method calls a class called xppSource and passes it the parameters that were supplied via the dialog.. In the xppSource class there was a method named parmMethod that accepts the parameters and creates the actual template.

So, all it takes is two simple methods. Once I create the two new methods for my display method template, it should be available on the right-click menu. Below is the code I used to create the template_method_display method in the EditorScripts class:

The above code creates a new dialog that requests that the user select a datatype and variable name for the display method, then passes that information to the xppSource class method "displayMethod" which I created:
This method takes the type name and the variable name from the previous dialog and formats it into a string and adds the word Display before them and the parens after "()". The code then creates a code block "{" and indents the code. Creates the return statement and ends the code block "}". 

In the end you get the following on your righ-click screen (ALT+R):

Then when you select display, you are greeted with the following dialog:
Then you must enter your desired values:
Finally, you select OK and you get your method template:

Once you create this template you can have a new display method in a matter of seconds and you can save yourself valuable keystrokes. Go ahead and have fun with the editorScripts class and increase your productivity!