Dynamics 365 Business Central: Execution order of subscriptions for the same event in different extensions (apps)

Dynamics 365 Business Central

Hi, Readers.
Today I want to share a test I did about execution order of subscriptions for same event in different extensions. I’m not sure it’s 100% correct, please contact me if you have better suggestions.

From the conclusion, the execution order of event subscriptions is related to the App id (Guid) of the extensions.

A few days ago, I was asked an interesting on Twitter.

Have two event subscriptions s1 and s2 in two different AL projects but those two were subscribed to same event publisher, now question is which subscriber execute first when the publisher triggered?

I was also very curious, so I went to investigate.

First, we can find following description in MS Docs.
Subscribing to Events:

There can be multiple subscribers to the same event from various locations in the application code. When an event is raised, the subscriber methods are run one at a time in no particular order. You can’t specify the order in which the subscriber methods are called.

https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-subscribing-to-events

And for this problem, the first thing that comes to my mind is the installation order of extensions and the order of object IDs. But as I tested it, I found that it actually has something to do with the App id (Guid).

Test Version:
SaaS Version: W1 19.4 (Platform 19.0.35391.36082 + Application 19.4.35398.35925)

Test event: Page trigger events

I prepared three PTEs.

1. PTE00:
App id: “bdab2184-be97-49f0-b36a-e3c8850d947e”
App name: “PTE00”
Object: codeunit 50149 EventHandler00
Message: ‘Extension: PTE00\Codeunit: 50149 EventHandler00’

2. PTE01:
App id: “b1cdc16c-e921-463d-b58d-27c1891f93ee”
App name: “PTE01”
Object: codeunit 50101 EventHandler01
Message: ‘Extension: PTE01\Codeunit: 50101 EventHandler01’

3. PTE02:
App id: “851372d9-0610-4e4e-9584-3ac5e5868f91”
App name: “PTE02”
Object: codeunit 50102 EventHandler02
Message: ‘Extension: PTE02\Codeunit: 50102 EventHandler02’

Installation Sequence: PTE00 -> PTE01 -> PTE02

Execution order: PTE02 -> PTE01 -> PTE00

So this has nothing to do with Object ID, but this looks like a reverse order of the installation sequence.

Let me do the next test.

Installation Sequence: PTE00 -> PTE02 -> PTE01

Execution order: PTE02 -> PTE01 -> PTE00
It hasn’t changed, so it shouldn’t have anything to do with the installation order either. But every execution is in a fixed order, why?

After several tests, I found that it has something to do with the app id.

PTE00: bdab2184-be97-49f0-b36a-e3c8850d947e
PTE01: b1cdc16c-e921-463d-b58d-27c1891f93ee
PTE02: 851372d9-0610-4e4e-9584-3ac5e5868f91

85… -> b1… -> bd…

To prove this assumption, I create an new extension.

4. PTE03:
App id: “8ad7d47c-9546-11ec-b909-0242ac120002”
App name: “PTE03”
Object: codeunit 50100 EventHandler03
Message: ‘Extension: PTE03\Codeunit: 50100 EventHandler03’

The last one installed PTE03.

The following are the app ids of the four Extensions.

PTE00: bdab2184-be97-49f0-b36a-e3c8850d947e
PTE01: b1cdc16c-e921-463d-b58d-27c1891f93ee
PTE02: 851372d9-0610-4e4e-9584-3ac5e5868f91
PTE03: 8ad7d47c-9546-11ec-b909-0242ac120002

85… -> 8a -> b1… -> bd…, the execution order should be PTE02 -> PTE03 -> PTE01 -> PTE00.

Test Video: Bingo!!!

This still wasn’t completely sure, so I got another extension and tried again. This time I published it directly from the VS code, because I was afraid that the installation method would be affected as well.

5. PTE04:
App id: “612e0043-05ff-422a-b4ff-658cd08e88a4”
App name: “PTE04”
Object: codeunit 50112 EventHandler04
Message: ‘Extension: PTE04\Codeunit: 50112 EventHandler04’

PTE00: bdab2184-be97-49f0-b36a-e3c8850d947e
PTE01: b1cdc16c-e921-463d-b58d-27c1891f93ee
PTE02: 851372d9-0610-4e4e-9584-3ac5e5868f91
PTE03: 8ad7d47c-9546-11ec-b909-0242ac120002
PTE04: 612e0043-05ff-422a-b4ff-658cd08e88a4

The execution order should be PTE04 -> PTE02 -> PTE03 -> PTE01 -> PTE00.

Test Video:

Okay, so, if you want your code to run before other extension, you might consider changing your app id, but please note that if you change the app id after installation, all data of your extension will be lost.

Give it a try!!!

PS: You can use Free Online GUID Generator to generate a new Guid.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

タイトルとURLをコピーしました