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...
Suggested Answer

Error while trying to capture error when running posting codeunits

(2) ShareShare
ReportReport
Posted on by 10
Hello.
In my development (I use Dynamics 365 BC Saas), I need to post sales documents, purchase, journal lines, transfer orders, etc and be able to capture the errors that occur during posting. If I call the posting codeunits as follows:
ok := PurchPost.Run(PurchaseHeader);
I get the following error: "An error occurred and the transaction is stopped".
From a one page action I call a custom codeunit that does a series of checks and updates and finally calls the posting codeunit.

Why does this error occur? Would there be any way to check if an error has occurred during registration?
I tried to use [TryFunction], but it doesn’t work very well, because if one line of the order gives error but the rest are correct, it registers all lines except the one that gives error.
Regards
Eva
I have the same question (0)
  • Suggested answer
    Steven Renders Profile Picture
    5,672 Moderator on at
    Did you try the debugger?
  • EJ-21020833-0 Profile Picture
    10 on at
    Yes, I launched the process with and without debugger.
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at

    Use RunWithCheck Instead of Run

    Most standard posting codeunits (e.g., Codeunit 90 for purchases, Codeunit 80 for sales) have a RunWithCheck method that performs pre-posting validations and throws detailed errors without committing the transaction. However, if you need to actually post and catch errors post-facto, Run is still your starting point—but you’ll need to enhance error handling.

  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,971 Super User 2025 Season 2 on at
    The posting codeunits handle transactions internally, so errors cause a rollback. If you need to capture errors, try using telemetry or debugging to identify the exact issue.
  • Suggested answer
    Suresh Kulla Profile Picture
    50,247 Super User 2025 Season 2 on at
    The codeunit.run will not throw an error as you are capturing the result, the error might be happening before this call.
  • EJ-21020833-0 Profile Picture
    10 on at
    Hello.
    Thank you all for your answers. I try to explain a little better what I need, since none of the answers solve my problem.
    I have webservices that receive information from external applications. Collect them in a custom tables, check that everything is correct, and if it is not correct I save an error message and mark the record as incorrect, and if all is correct, depending on the case, create an invoice and then post, or create a journal line and post. All this is automated with a project queue, although I can launch the process manually. The problem is that if there are any unchecked errors when the document post occurs, the project queue stops. 
  • Gerardo Rentería García Profile Picture
    25,390 Most Valuable Professional on at

    Hi, good day
    I hope this can help you, and give you some hints.

    Job queue - Business Central | Microsoft Learn

    Skipping that Record when it hits an Error in the Job Queue

    Best Regards
    Gerardo

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

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 764 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 635 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans