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
- Lower the cost of creating and upgrading customizations
- 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.
- The delegate keyword is used
- The return type must be void
- The body is empty
- 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
Step 2: Create a Consumer class
Create a Consumer class named Consumer and make an event handler for the delegate.
Step 3: Drag and Drop the event handler on the delegate
Step 5: 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
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.