This walkthrough uses a simple example scenario to demonstrate how to program events in the application.
The walkthrough illustrates to the following tasks:
- Creating an event publisher function to publish
an event.
- Adding code to application to raise the published
event.
- Creating an event subscriber function to
subscribe to a published event.
- Adding code to the event subscriber function to
handle the raised event
Prerequisites
To
complete this walkthrough, you will need:
- Microsoft Dynamics NAV 2016 with a developer
license.
- CRONUS International Ltd. demonstration database.
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. This procedure 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.
To create a new codeunit
1.
Open
the development environment, and then connect to the CRONUS International Ltd.
company.
2.
On the Tools
menu, choose Object Designer, choose Codeunit, and then choose New.
The C/AL
Editor opens.
3.
On
the File menu, choose Save.
4.
On
the Save window, enter 50000 in the ID field and enter My
Publishers in the Name field.
5.
Make
sure the Compiled check box is selected, and then choose the OK
button.
To create the event
publisher function to publisher the event
1.
On
the View menu, choose C/AL Globals.
2.
In
the C/AL Globals window, choose the Functions tab.
3.
In
a blank row, in the Name field, enter OnAddressLineChanged.
4.
To
open the properties for the OnAddressLineChanged function, select the
function, and then in the View menu, choose Properties. Set the
properties as follows:
1.
Set
the Local property to No.
2.
Set the Event
property to Publisher. This makes the function an event publisher.
3.
Set the EventType
property to Integration.
4.
Close
the Properties window.
5.
Add
a local parameter to the function for the address of the customer as described
in the following steps:
1.
On the Functions
tab, select the OnAddressLineChanged function, and then choose the Locals
button.
The C/AL
Locals window opens.
2.
On
the Parameters tab, in the Name field, enter line.
3.
Set
the DataType field to Text.
4.
Set the Length
field to 100.
NOTE: An event
publisher function cannot have a return value, variables, or text constants;
otherwise you will not be able to compile the function.
5.
Close
the C/AL Locals window.
6.
Close
the C/AL Globals window.
[IntegrationEvent]
OnAddressLineChanged(line : Text[100])









