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

Dynamics NAV 2013 & Expired Cronus License

Dynamics NAV 2013 & Expired Cronus License

We found an interesting problem - we were not able to run the development environment for Dynamics NAV 2013. Whenever we tried to run the development client, we got the following error message: "Your program license has expired" and the development client has closed...

read more
Indirect Dependencies and Access Modifiers

Indirect Dependencies and Access Modifiers

Last week, there was a discussion on Yammer on how to get values from the "Sent Email" record when all fields are marked as Internal. I was surprised that many people do not know what can/can't access modifiers (such as local, protected, or internal) be used for. I...

read more
AL Extensions: Replace Document Attachment

AL Extensions: Replace Document Attachment

I have published a new simple, open-source extension that allows replacing existing document attachments in all master entities as well as in open documents. The source code as well as the app file that can be installed in your environment is available on my GitHub...

read more
Clean up your copied environments

Clean up your copied environments

One of the most important things every developer should handle is to clean up the environment when the environment (or a company) is copied. Especially if the environment is managed directly by a client and they can create new copies anytime. Similarly, for copied...

read more
7 git commands you should know

7 git commands you should know

For many years, developers in C/AL did not need to know anything about Git or other versioning tools. That has changed with Business Central, or more specifically with AL Language. Today, we will look at the most important (and basic) git commands any developer should...

read more
How to define the source for item reservations?

How to define the source for item reservations?

It's not uncommon to have a customer's request to limit from which source items could be reserved. For example, customers may not want to reserve items from return orders. How can we achieve this goal? It's really simple. All the magic is done in the procedure...

read more
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

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