Skip to main content

Notifications

Announcements

No record found.

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

Catching an error in posting a sales order in an extension

(1) ShareShare
ReportReport
Posted on by 6
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
 
 
  • Wolter Kaper Profile Picture
    Wolter Kaper 6 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. 5,371 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 368 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,543 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans