António's DevLog

What are events in Business Central

Events were introduced in Dynamics NAV 2016.

But you’re asking — what are events?

In short, Events in Dynamics NAV/Business Central implement a Design Pattern known as the “Observer Pattern.”


Explanation

In Dynamics NAV/Business Central, you can create new functions and make them Events. An Event can be of type Subscriber or Publisher.

Publisher Event Types:

(You may also see Global and Trigger types, but you can only define the first two)

The difference?

What’s the Difference Between a Publisher and a Subscriber?

Think of the Subscriber as the response to a given situation monitored by the Publisher.


Built-in Events in NAV/Business Central

All table and page triggers like:

…have two default publishers:

Note:

Since a Publisher can have multiple Subscribers, execution order is not guaranteed.

So, if you want sub1 to run before sub2, you need to:


Practical Example

You might start with:

1// Sub1 and Sub2 both subscribing to the same event
2[EventSubscriber(...)]
3procedure Sub1(...)
4
5[EventSubscriber(...)]
6procedure Sub2(...)

After applying the workaround:

1// Only Sub1 remains, containing logic from both
2[EventSubscriber(...)]
3procedure Sub1(...)
4    // Logic for Sub1
5    // Logic for Sub2

The Observer Pattern

Let’s compare it to the Observer Pattern:

Class Diagram Focus:

class diagram

Subject:

Observer:

Sequence Diagram

sequence diagram

  1. Subject s1 adds Observer o1 and Observer o2 (attach/register).
  2. The monitored condition occurs (notifyObservers).
  3. Observer o1 is invoked.
  4. Then Observer o2.

Mapping to NAV/BC

Observer PatternDynamics NAV/BC
SubjectPublisher
ObserverSubscriber

TIP

You can check subscribers in NAV/Business Central via the Event Subscriptions page.

Example link (BC14 Web Client):

http://bc14/NAV/?company=CRONUS%20International%20Ltd.&page=9510

<< Previous Post

|

Next Post >>