After the “What’s New” webcast I’ve done two days ago, I’ve received questions on some new features introduced with Dynamics 365 Business Central 2021 Wave 2 (version 19) that (due to lack of time) I’ve presented quickly.
One of these features was the new way of raising errors that you can use with version 19, alias the Collectible errors.
I think that you have noticed that when you raise an Error() through code with Dynamics 365 Business Central version 19, like:
Error('Ahia! There''s an error here!');
you have now the following result:
If you click on the Detailed Information link, you can see some diagnostics useful if you need to open support tickets (like the Internal session id and the Application Insights session id). You can also click on the Copy to clipboard link:
and in this case you have the details loaded in your clipboard, completed with the entire call stack that originates the error:
Ahia! There's an error here! Internal session id: dec4d592-bda5-4150-9557-b8a03909e7a2 Application Insights session id: 33ead6d6-6450-474a-bdae-cb1015c0b440 Client activity id: ed1900dd-2202-4e41-94d4-352d26f72d68 Timestamp: 2021-10-07T18:47:15.9165138Z AL call stack: SDProcess(CodeUnit 50100).RaiseStdError line 2 - DemoApp by SD SDProcess(CodeUnit 50100).CustomProcess line 3 - DemoApp by SD CustomerListExt(PageExtension 50100)."CreateError - OnAction"(Trigger) line 25 - DemoApp by SD
This is extremely useful for troubleshooting your users problems. Just ask them to send the call stack and you can investigate the source of the problem without debugging.
With the standard Error() you’re breaking the current execution and the transaction is rolled back.
But imagine that you have a procedure that can throw different errors based on different conditions. In the past, your code stops working on the first error raised.
Now you can do something like in the following code:
Here I have a procedure called RaiseNewError where I’m throwing two different errors and each error has its own details. The procedure uses the ErrorInfo data type, that provides a structure for grouping informations about an error (this data type is supported for Business Central online from runtime 8.0).
The Error() here has an ErrorInfo object as parameter, where the Collectible property is set to true.
The procedure has also the following attribute:
ErrorBehavior(Behavior: ErrorBehavior)
that specifies the behavior of collectable errors inside the method scope.
What happens now if I execute this code?
This is the result:
A new error message appears on the screen, showing the first error of the chain together with a message that says that other errors are collected. If you click on the Detailed information, you can find the complete list of errors, completed with the call stack:
There are other methods that you can use for handling collectable errors and these methods are what I’ve described two days ago in the “What’s new” webcast. For your reference, they are the following:
Ok := System.HasCollectedErrors(): Gets a value indicating whether errors have been collected in the current error collection scope.
Errors := System.GetCollectedErrors([Clear: Boolean]): Gets all collected errors in the current collection scope (all errors marked as collectible).
System.ClearCollectedErrors(): Clears all collected errors from the current collection scope.
This new way of handling errors is extremely useful for troubleshooting problems (you can immediately retrieve the entire call stack) and also using collectable errors improves the general user experience on many processes.
Give them a try on your extensions… 🙂
NOTE: as said some days ago, please remember that the time of writing this post the Error(ErrorInfo) can be used only if your extension has target = Onprem but this is an AL language problem (aka bug) that will be fixed in the upcoming days.