Monday, October 13, 2014

Generate code using editor scripts and be more productive

Hi Friends,
Time is one of the most important factor in today's world. We develop many customizations for customers to save there time by reducing the number of clicks and automating the business processes.

There are tools available to save some time for developers too, to enable us to focus on coding the main logic rather than writing the pre requisite code to implement the logic.

One of such powerful feature is provided by editor scripts in AX. The good thing is that these classes can be enhanced to create your own custom code scripts. It is much more powerful then a template because you can add the extra pinch of run time magic.

Partners leverage these classes worldwide and extend them for variety of purposes. One of the very common use is to insert code comments. This is not a new tool and there are many good online resources already available. My intention is to share how we can use it to avoid overhead of writing too much of code.

So let us take an example. If you have to write X++ code to use a query class object for salesTable and have to add range on a field, then at-least you need to write 13-14 lines of code as shown below

By enhancing the code script classes you can achieve this just by a click of button! To achieve this I created 2 new methods in the system. One in EditorScripts class and another in XppSource class. After modifications I can see a new "query" option in editor scripts as shown below:

On selecting this option, system prompts me to enter the table name and the field name on which I want to apply the range.

Once I click OK, the code is automatically created for me.

Once I get the base code, I can further modify this as I want to implement my logic.

Below are the functions I added:
1. New method in EditorScripts class.

2. New method in XppSource class

You can download the XPO of the above modification from here.

The main idea is to save time by creating your own custom editor scripts which can help you to quickly generate pre defined code blocks at run time, so that you only spend time in writing the main logic.
Benefits of using this is approach:
1. Saves time and be more productive.
2. Avoid any typo errors.
3. You no longer need to shift your focus from your core logic to find the code pattern.
4. It is more efficient than Copy/Paste as you don't have to change the variable names and update all occurrences of those variables.

Some of the other ideas where you can extend these classes are:
  • Create find(), exists(), findRecID() methods on your tables. Refer to this nice blog by Joris de Gruyter here.
  • Create lookup methods.
  • Inserting code comments - here
  • Writing SQL statements which are required on day to day basis. For example: you can build your template for select statements using different keywords like crossCompany, firstonly , exits joins. etc.
  • Declaring/initializing set,lists, maps and there enumerators.
  • Create data contracts methods.
  • Declare Macros, Current versions, Current lists in class declaration.
  • Creating display methods - Refer to this nice blog by Micheal Brown here
If you are using these classes for any other purposes, please do leave your comment and share your bright ideas. 

No comments:

Post a Comment