Skip to main content

Notifications

Business Central forum
Suggested answer

Business Central AL Bypassing errors

Posted on by 10
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 61,112 Super User 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 10 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 10 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 45,274 Super User 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

Helpful resources

Quick Links

Contextual content recommendations & SharePoint Q&A

Instantly access relevant sales materials…

Beyond the DRA Cloud Label Printing

Explore cloud-based label printing within Dynamics 365…

HR Infrastructure Customer merge

Important considerations for HR Customer migrations…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,418 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,631 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,142

Featured topics

Product updates

Dynamics 365 release plans