Extensions V2 – communication between extensions via events

As a developer of an extension, I would like to give other developers the ability to customize the behavior of my extension in certain, predefined ways.

For instance, I could give a developer the ability to bypass my extension in certain scenarios. I also could give a developer the ability to customize some profit or other calculations and then pass the information back to my extension so that I can use it.

This is usually easily done using events. So, you can raise an event in your extension and have another extension subscribe to this event and perform some actions and pass data back. In extensions V1, it is difficult to do, because you actually need to provide the source code of your extension to everyone who wants to work with your events or with your data structure.

Extensions V2 or rather the modern development environment provides some better functionality here. You can actually add your events and raise them and then subscribe to them in another extension and also use their tables or other functionality without requiring the source code of the base extension. Visual Studio Code downloads the symbols for all dependent extensions. This means, as long you have the base extension installed in your development system and define this extension as a dependent extension, you can subscribe to the events.

I created a very simple sample project to explain this:

The base extension has a special page that has an action to raise an event. The page is shown here:

In the “OnAction” trigger for the action, I am raising the event, which is in a codeunit. The codeunit looks like this:

This is the entire base extension – I didn’t say, it actually would do anything useful, did I? You can find the code below as a zip file for download, so you can play with it.

Now, the other extension defines this “Events Test” extension as a dependent extension in the app.json:

I then created a codeunit that subscribes to the event and spits out a message.

So, what this means is that you can now allow your extensions to be customized, but only in the way that you want.

The issue is that, if you choose to do this, you have to provide some developer documentation so that you can explain your developers what the available events are, what their signature is, and what objects they are in. You then also have to explain to developers what they can and cannot do and you also have to account for issues when developers don’t do what you expect them to do and introduce additional issues.

It is not the best way of customizing some functionality, but it is a good way that works and provides you with the ability to make your extensions customizable.




Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.