Error actions for ErrorInfo data type

by Feb 1, 2023AL Language

Home 9 Development 9 AL Language 9 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:

Since its introduction, some improvements have been added, such as better handling of collectible errors. But there are still many things that can be improved, and one of them will be introduced in the upcoming major version of Microsoft Dynamics 365 Business Central 2023 wave 1 – Custom Actions in error messages!

How to add custom error action

To add a custom action to an error message, we need to create an error message using the ErrorInfo data type (see previous links). You can not add error action to errors with plain text.

To add the action, we have to use the ErrorInfo method ErrorInfo.AddAction(Text, Integer, Text) Method – Business Central | Microsoft Learn. This method accepts three parameters:

  1. Caption for the action
  2. Codeunit that contains methods that should be used when the action is selected
  3. Procedure from the codeunit from the previous point
    • The procedure must be public.
    • It must have one parameter of the ErrorInfo data type.

See the codeunit below.

codeunit 69900 "TKA Resolve It"
{
    procedure TrySolve1(ReceivedErr: ErrorInfo)
    begin
        Error('I cant!');
    end;

    procedure TrySolve2(ReceivedErr: ErrorInfo)
    begin
        Error('I do not know how...');
    end;
}

Now, we can add two actions to our custom error. ONLY TWO ACTIONS can be added as of now (31.01.2023) otherwise runtime error will be shown (but no warning in VS Code, but it is still really early to say if it will be fixed/shown in the VS Code).


local procedure TestError()
var
    MyErrorInfo: ErrorInfo;
begin
    MyErrorInfo.DataClassification := MyErrorInfo.DataClassification::SystemMetadata;
    MyErrorInfo.ErrorType := MyErrorInfo.ErrorType::Client;
    MyErrorInfo.Verbosity := MyErrorInfo.Verbosity::Error;
    MyErrorInfo.Title := 'Ouu noo!';
    MyErrorInfo.Message := 'Something terrible has happened.';
    MyErrorInfo.AddAction('Solve it!', Codeunit::"TKA Resolve It", 'TrySolve1');
    MyErrorInfo.AddAction('Why?!', Codeunit::"TKA Resolve It", 'TrySolve2');
    Error(MyErrorInfo);
end;

When we run our error, it will look like in the picture

below.

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

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