How to use Event Handler in Microsoft 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.

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.





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.

Happy Daxing J



7 comments:

  1. Many thanks for your help,

    in my case, i had to update the financial dimensions of the customer,

    as in the dimensions, customer account is available,

    and the requirement is to fill the same customer account that is being created

    into the financial dimension.

    tried to do it in the insert method, worked for Vendors to update the vendor account in the dimensions, but for customer it didn't work.

    when i tried the event handler option it worked like a charm, many thanks for

    your explanation.

    ReplyDelete
  2. Lighting, DJ and Projector is the main focus of the attraction for any type of events, as well as birthday, anniversary, marriage, etc. Contact us for complete event management services at affordable cost. av rental dallas | audio rental dallas | event lighting rental dallas | dj services provider in dallas | projector screen rental

    ReplyDelete
  3. thanks for the tips and information..i really appreciate it.. singapore event company

    ReplyDelete
  4. Nice Blog, with the Information about the AV RENTAL DUBAI To know More Information about the in Dubai CLICK HERE

    ReplyDelete
  5. Hi I read your blog and found that your blog is full of informative content. So keep posting thanks for share this article. Your article is very amazing. I like your article. thanks you ones again.

    mlsu ba 2nd year result name wise

    ReplyDelete
  6. Thank you so much for this beautiful article. I was looking for it helps me very much and also it will be very useful for those users who are looking for article about Large Business Event Cleaning

    ReplyDelete
  7. Very nice blog for needy people.

    Wish to see much more like this. Thanks for sharing your information. Dahercleaning services.!

    ReplyDelete

Powered by Blogger.