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...
Answered

Catching an error in posting a sales order in an extension

(0) ShareShare
ReportReport
Posted on by 33
Hi All,
 
I'm writing an extension that wants to do some changes to SalesLines just before posting a Sales Order.
For this purpose I subscribe to the OnBeforePostSalesDoc event in codeunit 80.
 
When the posting does not succeed, due to an error that is thrown in Microsofts part of the code, I want to undo my changes!
How to achieve this, is my question.
I've been looking for an event, like "OnPostingError", or something like it, but I have not found anything of the sort in either codeunit 80, 825 or table 55.
 
I also tried setting the "IsCommitSuppressed" parameter of the OnBeforePostSalesDoc to true, but it seems to be readonly (no "var" in front of it).
My hope was to extend the transaction to include my changes so that everything as a whole would be rolled back in case of error.
Microsoft does not seem to allow that.
 
So if both approaches are wrong, how does one undo ones changes in case of an error thrown in Microsofts part of the code...?
Any suggestions are appreciated.
 
Best regards,
Wolter Kaper
Entocare CV
 
 
I have the same question (0)
  • Ramiz Profile Picture
    597 on at
    Hi,

    I believe the standard process does roll back the changes if not committed.

    Can you share the change that you are doing and not rolling back.
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    20,814 Super User 2025 Season 2 on at
    To handle rolling back your changes when posting a Sales Order fails due to an error in Business Central, you can utilize the OnBeforePostSalesDoc event and manual error handling in your AL code.
  • Wolter Kaper Profile Picture
    33 on at
    Hi Ramiz,
     
    My event subscriber that's making the changes that are not rolled back, looks like this:
     
        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnBeforePostSalesDoc, '', false, false)]
        local procedure OnBeforePostSalesDoc(var SalesHeader: Record "Sales Header"; CommitIsSuppressed: Boolean; 
        PreviewMode: Boolean; var HideProgressWindow: Boolean; var IsHandled: Boolean; var CalledBy: Integer)
        var
            salesLine: Record "Sales Line";
        begin    
        //...
            salesLine.LockTable(true, false); //start a transaction - postpone commit till "after" listener has run!
            SalesLine.SetCurrentKey("Document Type", "Document No.");
            salesLine.SetRange("Document Type", SalesHeader."Document Type");
            SalesLine.SetRange("Document No.", SalesHeader."No.");
            if salesLine.Find('-') then
                 repeat
                      if salesLine."Location Code" <> SalesHeader."Location Code" then begin
                           //...
                           salesLine.Validate("Qty. to Ship", 0);
                           if not salesLine.Modify() then
                                error('Error updating sales line ' + Format(salesLine."Line No.") + ' for Sales document ' + SalesHeader."No.");
                      end;
                 until not (salesLine.Next() > 0);
            //...
        end;

        [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnAfterPostSalesDoc, '', false, false)]
        local procedure OnAfterPostSalesDoc(var SalesHeader: Record "Sales Header")
        begin
            //...
            commit();
        end;
     
    The "//..." means I left out stuff that I thought was unimportant. I can post the full code also but it would get a bit lengthy.
     
    The "OnAfter" subscriber contains a commit that is supposed to end the transaction that I started myself in the OnBefore subscriber.
    Maybe that detail is unimportant.
    If there is a commit in microsoft's part of the code I would expect my changes to be rolled back just as well.
    I'm curious why it does not do that. Any explanation is most appreciated.
     
    Best regards,
    Wolter Kaper
    Entocare CV
     
  • Verified answer
    Wolter Kaper Profile Picture
    33 on at
    Hi all,
     
    In the absence of further replies from the community I will summarize the solution that we reached ourselves, to the question I asked:
    We are now using 3 events, 2 from codeunit 80 Sales-Post, and 1 from the Sales Header Record:
     
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnBeforePostSalesDoc, '', false, false)]
    //This subscriber of ours does the changes that we want to undo if an error 
    // happens in Microsofts part of the posting process
     
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", OnAfterPostSalesDoc, '', false, false)]
    //This does stuff that we need done after a succesful post - it does not get
    // called after an error
     
    [EventSubscriber(ObjectType::Table, Database::"Sales Header", OnSendToPostingOnAfterPost, '', false, false)]
    //In our tenant this gets called after each posting attempt, succesful or
    // unsuccesful. We check if the posting failed, and in that case we undo the changes
    // done in the OnBefore… handler
     
    It is not ideal at all.
    If somebody has a better solution we are still interested.
     
    So in summary: 
    - we were not succesful in starting a transaction that would roll back our changes after an error happens in Microsofts part of the code
    - we found a useable event, but it has disadvantages
    (1. it always runs in our posting setup, but possibly not in others, 2. we have to test if the posting failed, instead of the event itself informing us)
     
    Best regards,
    Wolter Kaper
    Entocare CV
     

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

#2
YUN ZHU Profile Picture

YUN ZHU 663 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 515

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans