Microsoft Dynamics AX Eventing Framework – Coded and Automatic Events

Introduction to Event

Event is something that occurs at a particular time, sometimes expected sometimes not. Many events happen all around us in our daily lives. Some require action from us, some are not of our interest. Talking in technology terms, we can easily map event to the real-life concept of event. In Object-oriented paradigm, a class represents a real life entity and the methods are the operations or tasks that the entity can perform. An event can be said to represent something that happens within an object.

Terminology

In Microsoft Dynamics AX there are few terms that are used for events. The events terms in Dynamics AX are quite similar to that of .Net events.

Producer

The producer is the logic that contains the code that causes a change. It is an entity that emits events.

Consumer

The consumer is the application code that represents an interest in being notified when a specific event occurs. It is an entity that receives events.

Event

An event is a representation of a change having happened in the producer.

Event Payload

The event payload is the information that the event carries with it. When a person is hired, for example, the payload might include the employee’s name and date of birth.

Delegate

A delegate is the definition of the information passed from the producer to the consumer when an event takes place. You can add delegates as members of a class in X++. All delegates are protected members, so no access modifier is used, the return type is always void and the body must be empty.

Use and the application of Events

Events can be used to support the following programming paradigms:

  • Observation
  • Information Dissemination
  • Decoupling

The power of Eventing framework in MS  Dynamics AX

  1. Lower the cost of creating and upgrading customizations
  2. To support the programming paradigms

Types of Eventing in Dynamics AX

In Dynamics AX, eventing can be used in 2 ways depending upon the need and situation.

Coded Events

The coded events are the events that are raised manually by the use of delegates.

In X++, we can use delegates as a member of class. There are a few points that should be kept in mind while creating a delegate.

  1. The delegate keyword is used
  2. The return type must be void
  3. The body is empty
  4. No access modifier is attached with the method declaration

Implementing the Coded Events

The coded event requires the use of writing a delegate manually. The concept is quite simple.

For the demonstration purposes, we are going to use the Leave Management System in the Dynamics AX. I had modified the existing leave functionality and made a class “HCMEmploymentLeaveStatusChange” that will change the status of the leave once an employee’s leave is approved or disapproved.

Objective:

“Send Email notification to an Employee once his leave request is approved or disapproved.”

Solution

We know that we have a Consumer class and a Producer class. In this scenario, the Producer class is the HCMEmploymentLeaveStatusChange. We know that we write the delegate in the Consumer class and event handler for the delegate in the Producer class. Let’s get to the objective step-by-step

Step 1: Create a delegate in the Producer

Create a delegate in the producer class HCMEmploymentLeaveStatusChange and name it sendEmailToEmployee

Create a delegate in the Producer

Create a delegate in the Producer

Create a delegate in the Producer - Delegate method

Create a delegate in the Producer – Delegate method

Step 2: Create a Consumer class

Create a Consumer class named Consumer and make an event handler for the delegate.

Create a Consumer class - Eventing in Microsoft Dynamics AX

Create a Consumer class – Eventing in Microsoft Dynamics AX

Create a Consumer method - Eventing in Microsoft Dynamics AX

Create a Consumer method – Eventing in Microsoft Dynamics AX

Step 3: Drag and Drop the event handler on the delegate

Drag and Drop the event handler on the delegate

Drag and Drop the event handler on the delegate 

 

Event Handler - MS Dynamics AX

Event Handler – MS Dynamics AX

Event Handler - MS Dynamics AX

Event Handler – MS Dynamics AX

Step 5: Use the delegate to trigger the event\
Use the delegate to trigger the event

Use the delegate to trigger the event

 

Step 6: See it in action

When I approve or reject an employee leave request, the event is triggered with the help of the delegate. See below that method has been called and code inside the body has been run. We printed a info message and see it in action. J

 

Coded Events in action - Microsoft Dynamics AX

Coded Events in action – Microsoft Dynamics AX


Automatic Events

The events triggered by the environment when something happens are the Automatic events

There are 2 types of automatic events:

  • Pre Handler
  • Post Handler

As the name implies the pre handler is the event that occurs before the method and post handler is the event that occurs after the method. It is simple to make them by just changing the property “CalledWhen” of the handler to pre or post.  Each of the pre-event handlers can access the original values of the parameters and modify them as required. The post-event handlers can modify the return value of the called method.

Implementing the Pre & Post Events

Step 1: Create Event handler in the Consumer class

Create two event handlers

Step 2: Drag and Drop the Events into the method sendEmailtoEmployee

Step 3: Set property “CalledWhen” to Pre for EventHandler and Post for EventHandler1

Step 4: Write the logic in the methods eventHandler1 and eventHandler2

Step 5: See it in action

Now like I did it in the previous section. I did the same that is approved a leave request. Now what happens is that the same event handler is called but now it has a pre and post event handler. This is kind of a functionality that will help a person to customize it according to one own need without disturbing my code in the class.

Event Handlers

Event handlers are the methods that are called when the delegate is called, either directly through code (for the coded events) or from the environment (in the modeled events).

Event handlers are the methods that are called whenever an event takes place.

Other frameworks – Microsoft Dynamics

Eventing Dynamics AX 2012

Number Sequence Framework (Implementation)

Abubaker Siddiq Shekhani

Abubaker Shekhani is an IT Entrepreneur and Full Stack Developer. He is the co-founder and the Developer behind Mytabeeb, a health care solution. He has worked for 5 years in Microsoft Dynamics AX space with Techno-functional role and glad to be one of few Microsoft Dynamics AX developers/consultants in Pakistan. He is TEDx speaker and likes to speak in public. He is an Amateur Astronomer and Astrophotographer. He is the founding member of Karachi Astronomers Society. He can be reached at me@abubakershekhani.com.

You may also like...