Dynamics 365 Business Central: GetLastErrorCode, GetLastErrorText and GetLastErrorCallStack Methods

Dynamics 365 Business Central

Hi, Readers.
Last week, we briefly discussed how to view the last known error. More details: View the last known error.

Today, I’d like to dive deeper into how to retrieve the last known error directly from the AL code. For the Cloud/SaaS version, there are three main approaches:

So, you can use the System.GetLastErrorCode() Method method to identify the type of the last error that occurred. You can use the System.GetLastErrorText() Method to get the details of the last error. Let’s look at a simple example.

Error:

Get Last Error Code:

Another example:

Get Last Error Text:

By using this method, you can combine try methods/functions to add conditional checks based on the retrieved error messages. More details: How to use Try methods/functions (TryFunction Attribute)

PS: The result of the GetLastErrorCode method is not translated into the local language. The result of the GetLastErrorText Method is translated into the local language.

And we can use to System.GetLastErrorCallStack() Method gets the call stack from where the last error occurred.

For some errors, such as divide by zero errors and overflow errors, GetLastErrorCallStack does not return every call in the stack. To get the complete call stack for these types of errors, use the debugger and specify that you want to break on errors. On the Debugger page, in the Call Stack FactBox, you can view all the method calls that led to the error.

More details: Business Central 2025 wave 1 (BC26): Get the current callstack (SessionInformation Data Type)

Very simple, give it a try!!!😁

PS: Additionally, there is a method exclusive to the On-Premises version
System.GetLastErrorObject() Method: Gets the last System.Exception object that occurred. You use this method to retrieve and handle the last exception that occurred in the application.

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL