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

Notifications

Announcements

No record found.

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 58

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,397 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,362

#2
YUN ZHU Profile Picture

YUN ZHU 867 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans