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

BC Open Source? How to start?

BC Open Source? How to start?

BC Open Source? How to start? One of the most exciting news introduced last month in Lyon during Directions EMEA 2023 was the changes to the open-source initiative. This means that you can now contribute to the source code of the Base app and the System app, which are...

read more
Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What?

Validate a FlowField Field. Wait? What? There are not many things in the AL Language that surprised me. However, last week, I found one such thing - I reviewed customizations made by another partner and had to analyze the OOTB code of the Demand Forecast matrix. I run...

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

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