How to Set Up a Manual Event Subscriber in Business Central
An event subscriber is an AL method that listens to a specific event and determines which action to take when that event is raised.
Sometimes you need to subscribe to an event, but you need to control when that method code must be run. This can be done using the EventSubscriberInstance property as Manual.
In this way, you can determine when to activate your event subscriber methods.
When using the manual event subscriber, you will need to bind and unbind the subscriber codeunit.
The codeunit instance binds to the same exact instance and events will be raised on this instance.
If you define your subscriber codeunit as a local variable, once it goes out of scope, all the bindings are removed and you will not need to unbind it.
Let’s explain it with a practical example:
Setting up a Manual Event Subscriber | Example
Say you were asked to use an additional vendor remittance address for the check instead of the standard address.
These new fields were added to the vendor table and the vendor card page.
Now, you need to subscribe to the correct event in order to use the additional remittance address instead of the standard one.
If you check the code, you realize that there is an event to which you can subscribe and successfully place your code.
Notice that the variable Handled will determine if the standard code is executed or not.
The issue here is that this same function is called from many other reports and functionalities and all of them will be affected by it. Since that’s not what you want, you’ll need to manually subscribe the instance, setting the property EventSubscriberInstace = Manual.
Now, in the customized Check code, you bind/unbind the codeunit at the correct moment.
Let’s check the results.
If the check configured is a standard one, the new code should not be executed.
When printing the check, the standard address is shown as expected.
If the customized check is configured, the new code must execute.
When printing the check, the additional remittance address is shown as expected.