web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

GetLastErrorText returns different error

(0) ShareShare
ReportReport
Posted on by 94

GetLastErrorText appears to be returning text different from the last thrown error under some circumstances for one of our clients running BC 18.8 On Premises. We have the following code for a document:

...
Rec.SuspendStatusCheck(true);
Rec."Count Process Retries"  = 1;
Rec."Last Error" := '';
Rec.Modify();
recInterfaceEntry."Last Error" := '';
recInterfaceEntry.Modify();
Commit();
ClearLastError();
if not Codeunit.Run(recInterfaceSetup."Inbound Transfer Codeunit", recInterfaceEntry) then begin
    str := CopyStr(GetLastErrorText(), 1, MaxStrLen(str));

    recInterfaceEntry.Get(Rec."Interface Entry No.");
    recInterfaceEntry.WriteLog(str, recInterfaceEntry."Last Status"::TransferError);
    recInterfaceEntry."Last Error" := str;
    recInterfaceEntry.Modify();
    ...

The executed codeunit starts a posting process, during which an error such as "Nothing to post" will occur. The error text is immediately copied to the variable str, and then logs them in a protocol table. The other day, our client logged multiple entries with an empty text. After some research, we could determine the error that caused the logs to be due to an invalid posting date, but this has been logged correctly in the past. When debugging on a test server, I noticed that breaks between the error and the GetLastErrorText causes the returned error message to be different. The origin of that error is unknown since the debugger doesn't break for any error except the one I'm expecting, and the message itself contains the name of a field that never appears in the project of our extension. Given that the actual server was never debugged, some other factor must have been at play, but I suspect these two incidents are related.

First of all, does GetLastErrorText return errors from only one session, or can it not delineate the origin of the error? Microsoft's official documentation of the GetLastErrorText-command confirms neither statement.

Second, could there be a different reason as to why GetLastErrorText would suddenly return an empty string? There are no other sections in our extension that add anything else with this Enum value as parameter, so it had to have come from the command.

I have the same question (0)
  • DAnny3211 Profile Picture
    11,407 Super User 2026 Season 1 on at

    hi

    If you call the Codeunit.Run method to run a codeunit and an error occurs in the codeunit, then the error is displayed. However, if you also use the return value of the Codeunit.Run method, then the error is not displayed. In this case, you can use the GetLastErrorText method to determine whether an error has occurred and to see the text of the last error message that was generated. This example shows how to use the GetLastErrorText method. This example requires that you create two codeunits. Codeunit 50001 generates an error. Codeunit 50002 runs codeunit 50001 and if an error occurs, then it displays the text of the error.

    DAniele

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,986 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,071 Super User 2026 Season 1

#3
Dhiren Nagar Profile Picture

Dhiren Nagar 975 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans