Collectible Errors?! Is it already in use?

by Aug 3, 2022AL Language

Home 9 Development 9 AL Language 9 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 very first article about collectible errors from the last year ErrorInfo data type & Collectible Errors | Microsoft Dynamics 365 – Ing. Tomáš Kapitán (kepty.cz).

This article is focused on how ErrorInfo data type and collectible errors are used in Base App by Microsoft’s developers. Let’s start!

Is it already used somehow?

So lets start trying to find how many variables of ErrorInfo data type exist in the Base App. I used source codes from the Microsoft Dynamics 365 Business Central 2022 wave 1 (W1 version).

As you can see, there are only 8 (eight!) places where the ErrorInfo datatype variable is created. To be honest, I expected it will be already used much more as in my opinion this data type is undoubtedly useful.

Now let’s extend our search not only for data type but also for other places where ErrorInfo is used.

297 results in 24 files… This looks better, especially for functionality being in the system for only nine months now… But why are there only eight places including ErrorInfo data type, but so many more without created variable?

The reason for this difference is easy to find. When you check the documentation for ErrorInfo Data Type you find out that there are two methods (or only one, overloaded) that can be called on the ErrorInfo data type, not on the instance. Instance? What does it mean in AL Language? Instance in AL language is variable, so these two methods do not need a variable.

So in which processes have Microsoft already used ErrorInfo? Let’s see…

There are some internal or special objects (such as journal background check codeunits, integration objects, attachment objects, personalization, etc.). But there are also some essential objects such as Customer and Vendor table, Sales & Purchase headers, or Sales & Purchase Posting codeunits. Let’s check them.

For example, one usage of the ErrorInfo data type in the Customer table:

procedure CustBlockedErrorMessage(Cust2: Record Customer; Transaction: Boolean)
var
    "Action": Text[30];
    IsHandled: Boolean;
begin
    IsHandled := false;
    OnBeforeCustBlockedErrorMessage(Cust2, Transaction, IsHandled);
    if IsHandled then
        exit;

    if Transaction then
        Action := Text004
    else
        Action := Text005;
    Error(ErrorInfo.Create(StrSubstNo(Text006, Action, Cust2."No.", Cust2.Blocked), true, Cust2, Cust2.FieldNo(Blocked)));
end;

Surprisingly, the ErrorInfo is used as the parameter of the Error() method, in the same way as you can use the Text data type together with Error() method. But what is the reason why they used the Error(ErrorInfo) method instead of the Error(Text) method?

As you can see, there are 4 parameters used in ErrorInfo.Create(…) method. These parameters can enhance the information the users (or better, admins) can use to find out more information about the error. But also, there is the second parameter I already described in the previous part of this series – whether the error is collectible. In this example, the error IS collectible. That means when you (or Microsoft) write methods that collect errors, you can use the same method to loop through all customers and get the list of all blocked customers without raising the error.

Let’s try another example: Sales-Post codeunit.

In this example, there are much more places using the ErrorInfo data type. Many of these are TestField methods (check my previous article if you do not know how/why to use TestField(Field, Value, ErrorInfo) method instead of TestField(Field, Value) method). As ErrorInfo.Create() creates a collectible error, all these TestFields can be collected by the caller. And where are collectible processes?

It’s also easy to find places that collect errors, as all these methods need to be decorated with [ErrorBehavior(ErrorBehavior::Collect)] so… We can see there are only 7 objects (8 including base app tests) that collect errors. All these objects do background validation of inserted values to the document or journal and test the value. These tests include “posting preview” (not exactly the preview we know from the system, the process just tries to post the document, collects all errors that are raised during posting, and shows them to the user in the Factbox).

It still looks like there is plenty of processes where it can be very beneficial so let’s hope Microsoft will continue changing standard errors to collectible errors.

Recent Articles from the category

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
Substituting standard reports

Substituting standard reports

Report objects cannot be extended in versions previous to the Business Central 2021 release wave 1 version when ReportExtensions object was introduced. Since then, many of changes we need to do in reports can be done without creating a new copy of object. On the other...

read more
Isolated events

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...

read more
Custom Filter Tokens

Custom Filter Tokens

As a user of the Business Central you have some constants you can use to filter or insert values. These constants contain useful values for data manipulation such as t / today for date field - return current dateq / quarter for date field filters - return range of the...

read more
How to let users choose field(s) properly

How to let users choose field(s) properly

When some complex functionality is developed, it is sometimes necessary to let users choose a specific field. This can be required for field permissions, mapping imported values or any similar process. Earlier, the usual way was to create a link on the Field table...

read more
Working with Translation Files (XLIFF)

Working with Translation Files (XLIFF)

How does the translation file work? In Microsoft Dynamic 365 Business Central, displaying different languages is done using the XLIFF files extension (.xlf). The generated XLIFF files have strings that are specified in properties like Captions, Tooltips......

read more
1D & 2D Barcodes in Business Central 19.1

1D & 2D Barcodes in Business Central 19.1

Business Central (cloud-only!) includes one-dimensional (1D) barcode fonts since Microsoft Dynamics 365 Business Central 2020 wave 2 (17.0) was released in October 2020. It is a great improvement to how developers can handle requirements from their clients to print...

read more
ErrorInfo data type & Collectible Errors

ErrorInfo data type & Collectible Errors

A major change to AL Language development is here! Collectible errors can hugely improve any validation procedures. And what's new? Everything is about a new data type ErrorInfo. ErrorInfo data type ErrorInfo is a new data type introduced in Microsoft Dynamics 365...

read more
Exploring CosmosDB with Business Central, part 2

Exploring CosmosDB with Business Central, part 2

This part explains how to connect to Azure Cosmos DB from your Business Central extension using AL Language and how to get some values from the database.------------------------------------------------------------------------------------------------------------------...

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