Isolated events

by May 17, 2022AL Language

Home 9 Development 9 AL Language 9 Isolated events

With Business Central 2022 wave 1, a new setting for event publishers was introduced. Until this version, any error in any event subscriber caused interruption to the current running process and stopped the whole activity. In some cases (such as log-in), this is definitely unintended.

For this reason, Microsoft introduced Isolated Event Publishers. These events work the same as Codeunit.Run(..) – each call is executed in its own transaction, all errors are trapped, and only the event subscriber that caused the error is rollbacked.

To define a publisher as an Isolated Event, a new optional parameter was added to the publisher definition (see the image below).

Example

Let’s start with creating a new event publisher (nonIsolated – the third parameter in IntegrationEvent() is set to FALSE).

    internal procedure RaiseIsolatedEvent()
    begin
        OnSomething();
    end;

    [IntegrationEvent(false, false, false)]
    local procedure OnSomething()
    begin
    end;

And also create two event subscribers for our event.


    [EventSubscriber(ObjectType::Codeunit, Codeunit::"TST W1 Isolated Events", 'OnSomething', '', false, false)]
    local procedure FirstSubscriber()
    begin
        Message('Calling the first subscriber.');

        if true then
            Error('Error in the first subscriber.');
        Message('End of the first subscriber.');
    end;

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"TST W1 Isolated Events", 'OnSomething', '', false, false)]
    local procedure SecondSubscriber()
    begin
        Message('Calling the second subscriber;');
        if true then
            Error('Error in the second subscriber.');
        Message('End of the second subscriber.');
    end;

If you raise the event, only one of the subscribers is called – one message followed by an error message is shown.

Now let’s change the third parameter of IntegrationEvent(..) to true (to define the event as Isolated).

    [IntegrationEvent(false, false, true)]
    local procedure OnSomething()
    begin
    end;

If you raise the event once again, the behaviour is entirely different. Two messages without any error are shown, meaning no interruption to any running processing!

Conclusion

This new configuration for event publishers is an excellent way to manage external integration better. As a developer of an ISV solution or any solution extended by a third-party company, you may want to manage whether extensions can interrupt your processes (or define only specific events where error can happen).

Even more, with the combination of ClearLastError() and GetLastErrorText(), you can manage the errors in your processes much more easily – just clear the error before the event is called and check whether any error happened after all subscribers finished.

On the other hand, it also adds more complexity to the design of your solution. You should not make all your events Isolated (to be more specific, you should make the event Isolated ONLY if you really know the impact of this change), as after that, extending the application can not stop code from the executing!

Recent Articles from the category

NavigationAction for ErrorInfo data type

NavigationAction for ErrorInfo data type

One more article about ErrorInfo data type. Have you already read my previous posts about ErrorInfo and Collectible Errors? ErrorInfo data type & Collectible Errors | MSDyn365 Business Central - Ing. Tomáš Kapitán (kepty.cz) Collectible Errors?! | MSDyn365...

read more
Error actions for ErrorInfo data type

Error actions for ErrorInfo data type

It is already almost one and a half years since the ErrorInfo data type was introduced (we saw this data type for the first time in BC2021w2). If you do not know what it is and how to use this data type, check my previous posts: ErrorInfo data type & Collectible...

read more
Data types under the microscope: List

Data types under the microscope: List

The List data type in AL language represents an ordered collection of objects that can be accessed by their index. Unlike an Array data type, a List does not have a fixed size and does not need to have its dimension specified when it is declared. The List data type...

read more
Connect to Azure Function in BC 2022 wave 2 (v21)

Connect to Azure Function in BC 2022 wave 2 (v21)

The new version of the Microsoft Dynamics 365 Business Central brought a new system module "Azure Functions" that makes integration with Azure Functions much easier and straightforward. The typical scenario, why to use Azure Functions together with Business Central,...

read more
Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use?

Collectible Errors?! Is it already in use? This is the second part of my new article series about Collectible Errors. Let's check out the first part here: Collectible Errors?! | Microsoft Dynamics 365 - Ing. Tomáš Kapitán (kepty.cz) or you might be interested in my...

read more
Collectible Errors?!

Collectible Errors?!

Collectible Errors?! It has been already almost a year since ErrorInfo datatype & CollectibleErrors were introduced (I already have an article about basic structure: ErrorInfo data type & Collectible Errors). This article was released for the first time in...

read more

Sign Up for News

Certifications

Highest certification
Microsoft Data Management and
also in D365 Business Central

Microsoft Certified: Dynamics 365 Business Central Functional Consultant Associate

See other certifications here