Subscribing to events in runtime
In all examples of event subscription developed in the current chapter so far, subscription is registered statically at compile time. As soon as the event subscriber codeunit is saved and compiled, it starts to receive event notifications.
Subscription binding can also be controlled from the application code at runtime, so that the subscriber can start and stop subscriptions dynamically.
How to do it...
In this recipe, we will modify the codeunit 50904 Custom Integration Event
from the Creating custom events recipe to control the subscription state from the UI.
- Start the recipe from creating a new codeunit in NAV object designer.
- Open codeunit properties. To do this, press
Shift
+
F4
, or choose the option Properties in the View menu, from any place in the code editor. Change the value of the property
EventSubscriberInstance
from its default valueStatic-Automatic
toManual
. Close the properties window. - In C/AL Globals declarations, create a function...