Customizing Dynamics NAV
Discover different ways to customize the application to give you and your colleagues access to the features, functionality, and data that you need most.
2019 release wave 2 Discover the latest updates to Dynamics 365Release overview guides and videos Release Plan | Early Access Availability
Ace your Dynamics 365 deployment with packaged services delivered by expert consultants. | Explore service offerings
Connect with the ISV success team on the latest roadmap, developer tool for AppSource certification, and ISV community engagements | ISV self-service portal
The FastTrack program is designed to help you accelerate your Dynamics 365 deployment with confidence.
FastTrack Program | Finance and Operations TechTalks | Customer Engagement TechTalks | Talent TechTalks
Recall from my earlier post for description of Events – Events in C/AL Navision 2016
Here in this post we will see how to implement the same.
This could be helpful in situation where throughout the system you wish to have similar behaviour on specific condition. In general we will have to write code in every object with same set of logic, variables, and functions and so on.
Although still in this case too we will require to add code to every object, but only one time.
Think of you need to enhance or make changes in behaviour again you will have to go through each objects and do the required change. But using this feature we can keep it centralized and manage from single place, without going through each objects again.
Here below I am taking example one explained by Microsoft help basic concept, will come up with my version with some more effective usage later sometime.
When users change the address of a customer, you want to check that the address does not include invalid characters, which in this walkthrough 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.
Publishing the 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.
In this procedure we will add the event publisher function to a new codeunit, in which you can potentially add more event publisher functions for other events later.
Because you might want to change this event implementation in the future, you decide to create an integration event type.
The event publisher requires a single text parameter for handling the address of the customer.
Create a new codeunit
The codeunit for the event publisher I have created 50000 – Event Publisher.
Now we will add the event publisher function to publish the event.
To create the event publisher function to publisher the event
Setting this property makes the function available to be called from the other objects.
The C/AL Locals window opens.
An event publisher function cannot have a return value, variables, or text constants; otherwise you will not be able to compile the function.
The new function appears in the C/AL Editor with the following signature:
[IntegrationEvent] OnAddressLineChanged(line : Text)
You can now raise the event in the application.
Raising the Event
After creating the event publisher function to publish the event, now we will add code to the application to raise the event where it is required.
In this case, the event will raise when the Address field is changed on the page 21 Customer Card.
Therefore, we will add code to the Address – OnValidate() trigger in C/AL code of the page. Raising an event basically involves calling the event publisher function that publishes the event.
To raise the event
This calls the event publisher function to raise the event.
The event can now be subscribed to and handled.
Subscribing to and Handling an 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.
For example, in this case when a user changes the address of a customer (the event), you want code that checks that the value does not contain a plus sign.
Subscribing to and handling an event is accomplished by creating a C/AL function that is set up as an event subscriber and subscribes to a specific event (defined by an event publisher function). The event subscription function contains the application logic for handling the raised event.
In this case, we will create an event subscriber function that subscribes to the OnAddressLineChanged function in codeunit 50000 Event Publisher.
Unlike an event publisher function, an event subscriber function can only reside in a codeunit object. This procedure will add the event subscriber function to a new codeunit, in which you can potentially add more event subscriber functions for other events later.
To create a new codeunit
To create the event subscriber function to subscribe to and handle the event
This is the codeunit that contains the event publisher function (OnAddressLineChanged) that you want to subscribe to.
This field reads all the published events in the event publisher object.
When you get a message that asks whether you want to overwrite the edited function’s signature, choose Yes to continue.
LOCAL [EventSubscriber] CheckAddressLine(line : Text)
You can now add code to handle the event.
IF (STRPOS(line, ‘+’) > 0) THEN BEGIN
ERROR(‘Cannot use a plus sign (+) in the address [‘ + line + ‘]’);
This code checks the value of the Address field on page 21 Customer Card when is has been changed and returns a message if the value contains a plus sign.
Viewing the New Event Subscription
After you create an event subscriber, you can view information about it in page 9510 Event Subscriptions. This page provides information about all the current event subscriptions in the application. You can open this page directly from the development environment or from a Microsoft Dynamics NAV client.
To view the event subscription from the development environment
To view the event subscription from a Microsoft Dynamics NAV client
The Even Subscription Window will look like:
Testing the Event
To test the event implementation, you can run page 21 Customer Card from the development environment.
The following message appears:
Cannot use a plus sign (+) in the address .
 contains the value of the Address field.
I will come up with more details on this topic later in my future posts.
Business Applications communities