Use of REPEAT-UNTIL in Dynamics 365

Use of REPEAT-UNTIL in Dynamics 365:

(1) For finding sets of records in tables according to the filters and current key on tables in Business Central, use REPEAT-UNTIL with FINDSET function.

(2) REPEAT-UNTIL is defined as finding sets of records in the table according to the filter and current key. REPEAT UNTIL statement is executed from left to right, it is always executed at least one time, regardless of what the Condition is written.

(3) Note- REPEAT-UNTIL: only use this function only when you explicitly want to loop through a record set. You should only use this function in combination with the FINDSET function.

(4) Syntax of REPEAT-UNTIL is-

REPEAT 
<Statements> UNTIL <Condition>

(5) Let’s take an example, find sets of values and calculate its total number of documents in the Sales Header table by using REPEAT-UNTIL as shown.

Example-01: Total number of Sales Orders in Table.

Total number of Sales Orders in Table BC D365

Source Code:

codeunit 50001 “Custom Functions-01”
{
    trigger OnRun()
    begin
 
    end;
 
    local procedure HowToCountNoOfRecordsInTableinBCD365()
    Var
        ApplyFilteronSalesHeader: Record “Sales Header”;
        Counter: Integer;
    begin
        //Case-01 Use of REPEAT_UNTIL
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        if ApplyFilteronSalesHeader.FindSET then begin //Find Set of record in table
            repeat
                Counter += 1
            until ApplyFilteronSalesHeader.next = 0;
            message(‘Total Number of Sales Order No.: %1’, Counter);//Show all SO No. one by one
        end;
    end;
}

Example-02: Count only the first 10 sales order number.

Count only the first 10 sales order number in BC D365

Source Code:

codeunit 50001 “Custom Functions-01”
{
    trigger OnRun()
    begin
 
    end;
 
    local procedure HowToCountNoOfRecordsInTableinBCD365()
    Var
        ApplyFilteronSalesHeader: Record “Sales Header”;
        Counter: Integer;
    begin
        //Case-01 Use of REPEAT_UNTIL
        ApplyFilteronSalesHeader.reset; //Removing intial filters on table
        if ApplyFilteronSalesHeader.FindSET then begin //Find Set of record in table
            repeat
                Counter += 1
            until (ApplyFilteronSalesHeader.next = 0) or (Counter = 10); //It will exit when counter is 10
            message(‘Total Number of Sales Order No.: %1’, Counter);//Show all SO No. one by one
        end;
    end;
}

Use of FINDFIRST FINDLAST and FINDSET in BC D365
Use of SETRANGE and SETFILTER in Dynamics 365
How to Show Image or Picture in Report in D365 BC from Table Field
How to Create Sales and Receivables Setup in D365 BC
Understanding the General Ledger and the COA– Microsoft

Leave a Reply