web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Business Central AL Bypassing errors

(1) ShareShare
ReportReport
Posted on by 173
Hi Everyone,
Am trying to post multi-selected documents that is in a customized module but if the first document has an error the posting is stopping and the loop doesn't go to the second document which has no error. If all the documents have no error everything is working perfectly. I want a situation where by if the first document has an error, the posting should continue up to the last document and for the ones that have errors, the errors should be queued or reserved and display as a message after the posting process exhaust all the documents. If no errors the message should be all documents posted successfully. How can I achieve that in AL? Below is the extract of my code
 
action(/Post Proposal/)
                {
                    Caption = 'Post Proposal';
                    Image = Payment;
                    ToolTipML = ENU = 'Finalize the document or journal by posting the amounts and quantities to the related accounts in your company books.';
                    ApplicationArea = Basic;
                    Promoted = true;
                    PromotedIsBig = true;
                    PromotedCategory = Process;
                    PromotedOnly = true;
                    trigger OnAction()
                    var
                        Text001: label 'Are you sure you want to Post selected documents?';
                        Counter: Integer;
                    begin
                        Counter := 0;
                        if Confirm(Text001, false) then begin
                            CurrPage.SetSelectionFilter(Proposal);
                            if Proposal.FindSet() then
                                repeat
                                    Counter += 1;
                                    Rec.PostProposal(Proposal, false, Counter);
                                until Proposal.Next() = 0;
                            CurrPage.Update();
                        end;
                    end;
 
  • Suggested answer
    Mohana Yadav Profile Picture
    61,248 Super User 2026 Season 2 on at
    Move the Rec.PostProposal(Proposal, false, Counter); to a codeunit onrun trigger
    call the codeunit in your repeat until with if codeunit.run() then
  • CU29041409-0 Profile Picture
    35 on at
    To achieve the desired behavior where the posting process continues even if there's an error in one document and then displays a message summarizing the status of all documents, you can modify your code to handle errors within the loop and queue/reserve the errors to display them after all documents have been processed. Here's how you can modify your code in AL:
    action(PostProposal)
    {
    Caption = 'Post Proposal';
    Image = Payment;
    ToolTipML = ENU = 'Finalize the document or journal by posting the amounts and quantities to the related accounts in your company books.';
    ApplicationArea = Basic;
    Promoted = true;
    PromotedIsBig = true;
    PromotedCategory = Process;
    PromotedOnly = true;

    trigger OnAction()
    var
    ConfirmationMsg: Label 'Are you sure you want to Post selected documents?';
    Counter: Integer;
    ErrorMessages: Text[1000]; // Adjust the length as needed
    AnyErrors: Boolean;
    begin
    Counter := 0;
    ErrorMessages := '';
    AnyErrors := false;

    if Confirm(ConfirmationMsg, false) then
    begin
    CurrPage.SetSelectionFilter(Proposal);

    if Proposal.FindSet() then
    begin
    repeat
    Counter += 1;
    if not Rec.PostProposal(Proposal, false, Counter) then // Assuming PostProposal returns a boolean indicating success
    begin
    AnyErrors := true;
    ErrorMessages += 'Document ' + Counter.ToString() + ' encountered an error.' + ' | ';
    end;
    until Proposal.Next() = 0;
    end;

    CurrPage.Update();

    if AnyErrors then
    Error(ErrorMessages); // Display all error messages after processing all documents
    else
    Message('All documents posted successfully.');
    end;
    end;
    }
     
     
    I've added a variable AnyErrors to track if any errors occurred during the posting process. Inside the loop, after attempting to post each document, I check if an error occurred (Rec.PostProposal returns false in case of an error). If there's an error, I set AnyErrors to true and append an error message to the ErrorMessages variable. After processing all documents, I check if there were any errors. If there were, I display all the error messages. Otherwise, I display a success message.
  • KAYKAY Profile Picture
    173 on at
    I've tried your suggestion but I has the following errors:
     
    Operator 'not' cannot be applied to an operand of type 'None'ALAL0173 procedure PostProposal(Proposal: Record Proposal, Preview: Boolean, Counter: Integer)
     
    'Integer' does not contain a definition for 'ToString'ALAL0132  This happens on Counter.ToString()
  • KAYKAY Profile Picture
    173 on at
    I've tried your suggestion but I've not been able to implement  Counter += 1; in 
     
    codeunit 52000 PostProposalCodeunit
    {
        trigger OnRun()
        var
        Rec: Record Proposal;
         Counter: Integer;
        begin
            Counter := 0;       
            Rec.PostProposalBatches(Rec, false, Counter);
        end;
    }
     
    and also how do I queue all the errors such that after the loop completes run through all documents, I will display the errors?
     
  • Suggested answer
    YUN ZHU Profile Picture
    103,282 Super User 2026 Season 2 on at
    Hi, I think you can try the following.
    Dynamics 365 Business Central: How to use Try methods/functions (TryFunction Attribute)
     
     
    Hope this helps.
    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the August Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 715 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 452 Super User 2026 Season 2

#3
AndrewThomas81 Profile Picture

AndrewThomas81 371 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans