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:
page 50000 "Customer Test Page" { PageType = Card; SourceTable = Customer; layout { area(content) { group(GroupName) { field("No.";"No.") { ApplicationArea = Basic; } field(Name;Name) { ApplicationArea = Basic; } } } } actions { area(processing) { action(StartEvent) { CaptionML = ENU = 'Raise Event'; trigger OnAction(); var TestEvent : Codeunit "Event Test"; Handled : Boolean; begin TestEvent.OnTestEvent(Rec,Handled); Message(Format(Handled)); end; } } } var myInt : Integer; }
In the “OnAction” trigger for the action, I am raising the event, which is in a codeunit. The codeunit looks like this:
codeunit 50000 "Event Test" { trigger OnRun(); begin end; [IntegrationEvent(false,false)] procedure OnTestEvent(var Rec : Record Customer; var Handled : Boolean); begin end; }
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:
{ "id": "98d6dd65-17de-4b21-93ae-159d63089591", "name": "eventsubscription", "publisher": "Default publisher", "brief": "", "description": "", "version": "1.0.0.0", "compatibilityId": "1.0.0.0", "privacyStatement": "", "EULA": "", "help": "", "url": "", "logo": "", "capabilities": [], "dependencies": [ { "appId": "d1b9db01-bfe3-4cb3-bf09-bbab9f2079d1", "name": "EventsBase", "publisher": "Default publisher", "version": "1.0.0.0" } ], "screenshots": [], "platform": "11.0.0.0", "application": { "version": "11.0.0.0", "locale": "W1" }, "idRange": { "from": 50200, "to": 50249 } }
I then created a codeunit that subscribes to the event and spits out a message.
codeunit 50200 "Test Subscriber" { trigger OnRun(); begin end; [EventSubscriber(ObjectType::Codeunit,50000,'OnTestEvent','',true,true)] local procedure OnTestEvent(var Rec : Record Customer; var Handled : Boolean); begin Message('Customer: ' + Rec."No.") ; end; }
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.
Download: