When coding in AL, there are certain times when we need to avoid errors from stopping a specific process. So they have to be handled differently. In this post, you will learn 3 ways to handle errors in Microsoft Dynamics 365 Business Central.

  • Try Functions
  • Handle Codeunit.Run
  • Collectible Errors

Try Functions


The way to define a try function is simple. We just need to add [TryFunction] before the procedure.


The code inside a try function won´t stop in errors. And they all return a boolean. It will return true if the code runs successfully and false if it encountered an error. So, in the next image, only the error in the red square will be executed.


The main thing to know is that changes to the database within a try function aren’t rolled back. So, you shouldn’t include database write transactions inside a try function. Make sure to check Stefano´s post talking about it:

However, we can include validations. In this example, we try to validate an invented payment method that does not exist in the database.


As you can see in this video, once the payment method is created, it can be modified.


Get more information about collectible errors here:

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-error-collection


Handle Codeunit.Run


This method was the first error-handling mechanism for older versions of Business Central. The way to use it is similar to a try function.

By executing Codeunit.Run, it returns a boolean. Returning true, if it was successful, and false if it encountered an error.

You can find a simple example here:


In the base app, we can also find some examples. In this image, Codeunit.Run is used for releasing a purchase document. If it succeeded, it returns true. Otherwise, it returns false.


Get more info about this way of handling errors here:

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/codeunit/codeunit-run-method

Collectible Errors


Collectible errors were introduced in BC19 (2021 Wave 2). It´s a way to get multiple errors while the code is executing. In this scope, the process won´t stop at errors and will continue until it finishes.

First, we have to define the error behavior property before the procedure name.


Then, we need to define the ErrorInfo data type. And add the properties you can see in the image below.

In this example, emails are sent in a loop. If an email couldn´t be sent, an error is collected.

Finally, the code continues until the process is finished. Note that the line Error(ErrInfo) doesn´t stop the process. It´s only been collected for later use.


After the final message and once the process is finished, we can see all the errors in the message that it´s displayed. With ErrorInfo.Create we can define any error message:


If you want to learn 5 ways to send e-mails in Business Central follow the article below:


Get more information about collectible errors here:

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-error-collection

For the full information about error handling strategies follow the Microsoft documentation below:

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-al-error-handling


That´s all. Hope you find it helpful.

3 Ways To Handle Errors In Business Central

Post navigation


Leave a Reply

Your email address will not be published. Required fields are marked *