(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: onlyuse 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.
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.
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; }