13 September 2016

Implementing Events in NAV 2016, NAV 2017

Hi All,

You can use Events to design the application to react to specific actions or behavior that occur. Events enable you to separate customized functionality from the application business logic. By using events in the application where customizations are typically made, you can lower the cost of code modifications and upgrades to the original application.
  • Code modifications to customized functionality can be made without having to modify the original application.
  • Changes to the original application code can be made with minimal impact on the customization's.
How Events works :
  • An event is the declaration of the occurrence or change in the application. An event is declared by a C/AL function, which is referred to as anevent publisher function. An event publisher function is comprised of a signature only and does not execute any code.
  • publisher is the object that contains event publisher function that declares the event. The publisher exposes an event in the application to subscribers, essentially providing them with a hook-up point in the application.

    A subscriber listens for and handles a published event. A subscriber is a C/AL function that subscribes to a specific event publisher function and includes the logic for handling the event. When an event is raised, the subscriber function is a called and it code is run. 


Below is a small demonstration of using Events

Ex : When users change the address of a customer, you want to check that the address does not include invalid characters, which in this case is a plus sign (+). To accomplish this, you will publish an event that is raised when the Address field on page 21 Customer Card is changed. To handle the event when it is raised, you will add an event subscriber function that includes logic that checks the address value and returns a message to the user if it contains a plus sign.

1) Creating an event publisher function to publish an event

To publish an event, you create a C/AL function that is set up to be an event publisher. An event publisher function can be added in any object, such as a codeunit, page, or table.

I have taken Customer table as an Event publisher object but Codeunit should be preferred.


Now create a Global function named  "OnAddressLineChanged" and set the property as shown below.





Add a Local parameter to the function as shown below


The new function appears in the C/AL Editor with the following signature:



2) Adding code to application to raise the published event.

After Event Publisher function is created, now will raise the Event on Customer Card Page 21 OnValidate Trigger(). 

I have created a Global variable"recCustomerPublisher" for Customer table where the Event Publisher function resides.




3) Creating an event subscriber function to subscribe to a published event.

Once an event has been published you can add code to the application that subscribes to and handles the event when it is raised.

Unlike an event publisher function, an event subscriber function can only reside in a codeunit object

Please create a new Codeunit My Subscriber  and add function "CheckAddressLine" as shown below



Please set the Properties for the function as shown below



When you get a message that asks whether you want to overwrite the edited function's signature, choose Yes to continue


4) Adding code to the event subscriber function to handle the raised event

Now you can see the function added in code editor. Please add the code To handle the event as shown in the screenshot.






Now try to enter '+' sign in the Address field you will get the below Message. 



You can view information about all added Event Subscriptions in page 9510 Event Subscriptions






Thanks & Regards,
Nandesh Gowda

No comments:

Post a Comment