Dynamics Search Engine

Sunday, May 27, 2012

How to use Event Handler in Microsoft Dynamics AX 2012

How to use Event Handler in Microsoft Dynamics AX 2012

Applied on:
Microsoft Dynamics AX 2012 CU2.

Assumption:
This article is for those who has at least basic data flow, navigation and programming
knowledge of Dynamics AX.

About this article:
This article may answers your question like:-

• 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.



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.

There are different programming paradigms like:
Observation: Where based on a particular behavior occurrence events can be used. For example if a new customer is created, based on this you need to update a table record and intimate to user.

Information dissemination: This is related to trigger an event at right time, right information to get expected result.

Decoupling: One producer's event can be acted upon by any number of consumers. Consumers can act upon any number of events from many different producers.


You should locate natural customization points and use events to publish the relevant information to subscribers.

You should invoke / defining 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.


5) Right click on class CustTableEventHandler then New then Pre- or post-event handler as shown below.


6) Once you click on this, system gives you a method as shown below.


7) Customize the method as shown below.

8) Here args is providing current record to custTable instance and info is displaying the current customer account. The code snippet is below.

public static void custCreateInfo(XppPrePostArgs _args)
{
     CustTable custTable;
     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.

14) Go to Customer form and create a new customer. Here I created a new customer account called “Test-000001”.


15) Once this customer is created system will give your infolog as shown below.


Hope this will help you to understand the event handler in Microsoft Dynamics AX 2012.

9 comments:

  1. This article can guide you how to develop event in Dynamics AX 2012.

    ReplyDelete
  2. Good post, I'm trying the same in AX 2012 R3 but doesn't seems to be working. Any idea how to make it work on R3?

    ReplyDelete
  3. It's working in R3 as well. What is the error you are getting?

    ReplyDelete
    Replies
    1. Let me know if you can help me out here.

      Delete
    2. It is working, I've to close and reopen AX. Thanks a lot Sir.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Nice one, Good information.Thank you.

    ReplyDelete