Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

GetLastErrorText returns different error

Posted on by 10

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.

  • DAnny3211 Profile Picture
    DAnny3211 9,272 Super User 2024 Season 1 on at
    RE: GetLastErrorText returns different error

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,219 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,056 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans