Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Transaction scope in Business Central

(1) ShareShare
ReportReport
Posted on by 4
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.
  • MO-19112130-0 Profile Picture
    MO-19112130-0 4 on at
    Transaction scope in Business Central
    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
    Alexander Drogin Profile Picture
    Alexander Drogin 123 on at
    Transaction scope in Business Central
    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.
     

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,414 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans