Skip to main content

Notifications

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

Catching an error in posting a sales order in an extension

(1) 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
 
 
  • Verified answer
    Wolter Kaper Profile Picture
    Wolter Kaper 33 on at
    Catching an error in posting a sales order in an extension
    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
     
  • Wolter Kaper Profile Picture
    Wolter Kaper 33 on at
    Catching an error in posting a sales order in an extension
    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
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    Khushbu Rajvi. 7,593 Super User 2025 Season 1 on at
    Catching an error in posting a sales order in an extension
    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.
  • Ramiz Profile Picture
    Ramiz 474 on at
    Catching an error in posting a sales order in an extension
    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.

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,971 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,846 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans