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

Transaction scope in Business Central

(3) ShareShare
ReportReport
Posted on by 30
Hi everybody.
 
It seems there is a BIG Design flaw on the very basic level in Business Central Architecture if Im right in the following.
 
 
The scenario goes as
 
1) External system invokes Codeunit webservice in Business Central (latest version)
2) CodeUnit webService stores the Payload in a table with status "Received" and Commits
3) Codeunit webservice executes the business logic (inserting, modifying records) in a new transaction
4) Step 3) fails in some way (The business logic is only using collectible errors).
5) If any collectible errors have been found - ROLLBACK all database updates done in 3)
6) Save all collected errors in a record.
 
This is a pretty basic way of creating webservices in every other programming language that Im using. Somehow this simple pattern does not seem to be possible in BC ??
 
When saving all the collected errors in 6) business central is also saving all intermediate database record inserts/modified as part of 3)
But I want to rollback those and simply store the error messages.
 
If this it not possible I would rather say that BC is not mature in any way hence lots of business central instances must have weird/not complete data stored?
 
I must be missing something here - so any help is appreciated.....
 
BR Morten
 
 
 
UPDATE 16/01/2025
What Alexander is mentioning below is correct and the standard way of executing logic in an independent transaction.
This pattern was followed in my code BUT as part of my code I was calling the procedure "SendToPosting" on the SalesHeader table.
Thats DANGEROUS because inside this method an explicit COMMIT is executed, forcing the surrounding transaction to be comitted.
That was the culprit.
I have the same question (0)
  • Suggested answer
    Alexander Drogin Profile Picture
    262 on at
    You can wrap the business logic in another codeunit and call that codeunit from your web service via its OnRun trigger. So your published web service codeunit, will be running a statement: "if LogicCodeunit.Run() then...".
    This way, when a codeunit is invoked via the Run method, and if the return value of this method is captured in the calling code, the inner codeunit runs in a separate transaction which is rolled back in case of an error. The caller can save and commit the result, while everything inside the callee will be rolled back.
     
    So your step 5 can be that wrapper codeunit that throws a "normal" non-collectible error if any errors are collected during the execution.
    Maybe it's not quite obvious and looks like a hack, but this is the way Business Central manages nested transactions.
     
  • MO-19112130-0 Profile Picture
    30 on at
    Hey Alexander
     
    What you are suggesting is the exact pattern Im following. I'll go back and re-evaluate my code - I must be missing something
     
    Thanks for the answer though
     
    Br Morten
  • Suggested answer
    YUN ZHU Profile Picture
    95,763 Super User 2025 Season 2 on at
    Just adding some information:
    Dynamics 365 Business Central: Method Commit Behavior (CommitBehavior Attribute)
     
    Thanks.
    ZHU

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