Nav 2016 Event Timing

stopwatch-911767_1280

With Dyanamcis Nav 2016 you now have the ability to make events. While this makes creating add-ons much easier and minimize the impact on existing code. It might become harder to debug though. How do you know what functions are subscribed to your event? What order do these responses happen? I wanted to figure out if there is an order. I am going to go through a small example of creating an event on a page. Then make multiple subscribers. One on a table, one on the same page, a sub page, and a few within codeunits. I want to see which ones will fire first.

The Setup

Create a table with one field.

Create a page with the table you just made.

Create an event on this page that you can subscribe to.

Create an action on this page to fire off the event.

Create a function on the page and table to subscribe to your new page event. Now this is where I learned something new. You cannot create event subscribers on pages or tables.  This is a really good design constraint by Microsoft. Because of this, all your subscribers must be in codeunits, so at least that solves some of the mystery of what event subscriber fires first. But, there is still the question of multiple subscribers from the same codeunit or different codeunits. Which ones go first?

So let make a few more things for our experiments.

First, a codeunit with three subscribing functions to the same event. Each one with a slightly different message so we know which one goes off first.

Event Subscribers

Next, lets make an other codeunit with another subscriber function to see what codeunit will get fired first.

Events

Ok time to see what happens.

Untitled2

So as its seems right now, they are ran in order they are made. What about changing their function IDs? No luck, still the same order. What if we add yet another subscriber to the first codeunit we made? This time all subscribers in the first codeunit ran before the second codeunit. There for we could assume that events are ran in order from lowest codeunit ID to highest ID. And then within a codeunit they are ran in the order they are presented, regardless of ID.

This might not seem like a big deal determining exactly the order of subscribers to an event. But if you ever write subscribers that rely on each other you could be in for some big trouble. Also, knowing the order can help save a lot of time with trouble shooting and debugging.

Results

Event subscribers are only available in codeunits, they are ran in order of lowest codeunit ID to highest. Within a codeunit the subscribers run top to bottom, regardless of ID.

 

One thought on “Nav 2016 Event Timing

  1. Nice to find this article – thanks for taking the time to write about this, events are a big story in NAV. But be careful – the order in which subscribers are being fired is not guaranteed, so don’t base any important business logic on it.

Leave a Comment