Skip to main content

Notifications

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

Business Central AL Bypassing errors

Posted on by 28
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
    YUN ZHU Profile Picture
    YUN ZHU 71,565 Super User 2024 Season 2 on at
    Business Central AL Bypassing errors
    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
  • KAYKAY Profile Picture
    KAYKAY 28 on at
    Business Central AL Bypassing errors
    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?
     
  • KAYKAY Profile Picture
    KAYKAY 28 on at
    Business Central AL Bypassing errors
    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()
  • CU29041409-0 Profile Picture
    CU29041409-0 33 on at
    Business Central AL Bypassing errors
    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.
  • Suggested answer
    Mohana Yadav Profile Picture
    Mohana Yadav 58,880 Super User 2024 Season 2 on at
    Business Central AL Bypassing errors
    Move the Rec.PostProposal(Proposal, false, Counter); to a codeunit onrun trigger
    call the codeunit in your repeat until with if codeunit.run() then

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans