Event definitions

As we mentioned previously, events are fundamental building blocks when it comes to developing extensions for Dynamics 365 Business Central. When working with events, we have two main entities: the event publisher and the event subscriber.

An event publisher (an event that's raised by the application) can be defined in AL by using the teventbus (for a business event) or teventint (for an integration event) snippets:

A business event has the following schema:

[BusinessEvent(IncludeSender)]
local procedure MyProcedure()
begin

end;

Here, IncludeSender is a Boolean value that specifies whether the global methods defined in the object that contain the event publisher method will be visible to the event subscriber methods that will subscribe to this event (this is true if the global methods must be visible and false (the default value) if not).

When the IncludeSender argument is set to true, the signature of the event subscriber methods that will subscribe to this published event will automatically include a VAR parameter (a reference value) for the published event object.

An integration event has the following schema:

[IntegrationEvent(IncludeSender,GlobalVarAccess)]
local procedure MyProcedure()
begin

end;

Here, the IncludeSender Boolean parameter has the same meaning as we described previously.

GlobalVarAccess is a Boolean parameter that specifies whether the global variables defined in the object, which contains the event publisher method, are accessible to the event subscriber methods that subscribe to this published event (this is true if they must be exposed and false – which is the default value – if not).

When the GlobalVarAccess argument is set to true, all the event subscriber methods that subscribe to this event will be able to access the global variables in the object where the event publisher method is declared. You have to manually add the variable parameters to the event subscriber methods, and you need to use a name and a type that match the variable declaration in the event publisher object.

After an event has been published by an event publisher (your previously defined method), you need to raise that event in your code where needed (event subscribers will not react to the event until it's raised in your application code).

As an example, the following is a codeunit with a public method that raises a business event and an integration event:

codeunit 50100 MyCodeunit
{
procedure CheckIfPacktCustomerIsEnabled(CustomerNo: Code[20]): Boolean
begin
//Raising a business event
MyBusinessEvent('XXX');

//Other code here...

//Raising an integration event
MyIntegrationEvent('YYY');
end;

[BusinessEvent(true)]
local procedure MyBusinessEvent(ID: Code[20])
begin
end;

[IntegrationEvent(true,true)]
local procedure MyIntegrationEvent(ID: Code[20])
begin
end;

//Global variables
var
myInt: Integer;
Customer: record Customer;
}

An event subscriber (a function that handles a raised event in the application) can be declared using the teventsub snippet:

[EventSubscriber(ObjectType::ObjectType, ObjectID, 'OnSomeEvent', 'ElementName', SkipOnMissingLicense, SkipOnMissingPermission)]
local procedure MyProcedure()
begin

end;

From the preceding code, we can see the following:

  • ObjectType is an enumeration that identifies the object type that publishes the event to subscribe to (the object that contains the event publisher method) or that raises the trigger event to subscribe to.
  • ObjectId is an integer value that specifies the ID of the object that publishes the event to subscribe to (when declaring it, don't use the ID; use the ObjectType::Name syntax).
  • OnSomeEvent is a text parameter that specifies the name of the method that publishes the event in the object identified by the ObjectId parameter.
  • ElementName is a text parameter that's used for database trigger events. It specifies the table field that the trigger event pertains to.
  • SkipOnMissingLicense is a Boolean parameter that specifies what happens to the event subscriber method when the Dynamics 365 Business Central license of the user account that runs the current session does not include the permissions on the object that contains the subscriber method (true if the method call must be ignored and false if an error must be thrown and the code's execution must be stopped).
  • SkipOnMissingPermission is a Boolean parameter that specifies what happens to the subscriber method when the user account that runs the current session does not have permission on the object that contains the event subscriber method (true if the method call must be ignored and false (the default value) if an error must be thrown and the code execution must be stopped).

As an example, this is a codeunit with two event subscribers for the business and integration events we defined in the previous example:

codeunit 50101 MySubscriberCodeunit
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::MyCodeunit, 'MyBusinessEvent', '', false, false)]
local procedure MyBusinessEventSubscriber(ID: Code[20])
begin

end;

[EventSubscriber(ObjectType::Codeunit, Codeunit::MyCodeunit, 'MyIntegrationEvent', '', false, false)]
local procedure MyIntegrationEventSubscriber(ID: Code[20])
begin

end;
}

When defining the event subscriber, if you press Ctrl + spacebar on the event parameters, you will see a list of the objects that the event can interact with (exposed by the publisher). In our example, the business event subscriber can see the event parameter and the sender object (because we've declared the event publisher with IncludeSender set to true), as follows:

The integration event subscriber can see the event parameter, the sender object (because we've declared the event publisher with IncludeSender set to true), and the global variables of the sender object (because we've declared the event publisher with GlobalVarAccess = true):

When using events, always remember the following:

  • When the code that calls the event publisher method is run, all the event subscriber methods that subscribe to the event are run.
  • If there are multiple subscribers, the subscriber methods are run one at a time in a random order (there's no way to specify the order in which the subscriber methods are called).
  • If there are no subscribers to the published event, then the line of code that calls the event publisher method is ignored and not executed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.95.244