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 Errors | MSDyn365 Business Central – Ing. Tomáš Kapitán (kepty.cz)
- Collectible Errors?! | MSDyn365 Business Central – Ing. Tomáš Kapitán (kepty.cz)
- Collectible Errors?! Is it already in use? | MSDyn365 Business Central – Ing. Tomáš Kapitán (kepty.cz)
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:
- Caption for the action
- Codeunit that contains methods that should be used when the action is selected
- 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.