Microsoft incorporated lot of beautiful new features in Dynamics AX 2012 and one of them is Event Handler. It’s a very nice feature of Dynamics AX 2012 which allows you trigger an event after or before an activity.
So today am going to tell you about these features:
In Microsoft Dynamics AX 2012 how to handle event on different methods / occurrences.
In Microsoft Dynamics AX 2012 how to implement or apply pre or post event handler.
How to develop an event handler step by step in Microsoft Dynamics AX 2012.
What is new in Microsoft Dynamics AX 2012 from programming point of view?
X++ Event handling in Dynamics AX 2012.
You should invoke events over using pre or post events on methods.
Pre-Event Handlers and Post-Event Handlers
An event handler can reside underneath a method node can run either before or after the method runs. You can use CalledWhen property on the event handler node. The CalledWhen property has two values:
• Pre – The event handler runs before the method starts.
• Post – The event handler runs after the method ends.
A new class the XppPrePostArgs Parameter is being used by event handlers.
A pre-method event handler that has only an XppPrePostArgs parameter can inspect and change the values of the parameters.
Similarly a post-method event handler that has only an XppPrePostArgs parameter can inspect and change the return value from the method.
When an XppPrePostArgs object is used, the values of the parameters and the return type can be changed by the event handler. The values can be changed even if the parameters and return type are value types, such as an integer or string. If a parameter or a return type is a reference to an object, the event handler can call methods on the object and could change the state of the object.
Event handlers can run only on the same tier as the publisher class of the delegate runs on. For instance, if the publisher class has its RunOn property set to Server, to declare a method with client keyword and call a subscribed event handler method is not allowed.
You can use X++ event as well as .Net made managed code event. You can define it at Event handler proper called EventHandlerType.
How to use event handlers in Microsoft Dynamics AX 2012 step by step:
1) Open your new developer work space and go to AOT then classes node.
2) Right click on Classes node and click on New Class as shown below.
3) By default system will give a name to it. Here in my case it’s Class1. Right click on newly created class and click on Rename shown below.
4) After clicking Rename, give a name called CustTableEventHandler to it. Here I am going to develop an event to be applied on CustTable so that is the reason why I decided this name (CustTableEventHandler). After renaming this class, it looks as shown below.
6) Once you click on this, system gives you a method as shown below.
public static void custCreateInfo(XppPrePostArgs _args)
custTable = _args.getThis();
info(strFmt(“Customer account %1 has been created”, custTable.AccountNum));
This method I support to call from insert method of CustTable with type post event. It means that once insertion is done to CustTable, system will display recently inserted customer account number. It depends on your business requirement what logic you want to apply here. So here you can develop your required business logic or invoke pre built logic.
9) Go to Tables node in AOT then find out CustTable.
10) Go to insert method of CustTable and right click on it then click on New Event Handler Subscription as shown below.
11) After clicking you will get a new Event handler shown below.
12) Rename the event handler to custCreateInfo and set the property as shown below.
13) Now save your work.