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

Community site session details

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

Rec1.Insert() inside a repeat/until loop through FindSet() set of records

(6) ShareShare
ReportReport
Posted on by 103
Hi,
 
I cannot understand the behaviour I am facing in my code. 
 
Let's say I am working in Table1. I filter records, apply a FindSet(true), loop through set of records with a repeat. In the loop, I am calling a procedure that is adding a new record in Table1. What happens is that, the next record from the FindSet() set of Records, is this newly created record ! 
 
My understanding is that the FindSet(true) method list a static set a record, no matter what happens within each repeat iteration.
 
Anybody has an idea ?

    procedure proc1()
    var
        PurchPrice: Record Table1;
    begin
        PurchPrice.SetRange("Vendor No.", 'V001');
        if PurchPrice.FindSet(true) then begin
            repeat
                if PurchPrice."Starting Date" = Today then
                    proc2();
            until PurchPrice.Next() = 0;
        end
    end;
   
procedure proc2()
    var
        purchPrice: Record Table1;
    begin
        purchPrice.Init();
        purchPrice.Validate("Item No.", 'itemNo');
        purchPrice."Vendor No." := 'V001';
        purchPrice."Starting Date" := Today + 365;
        purchPrice.Insert()
    end;
I have the same question (0)
  • Suggested answer
    Ramesh Kumar Profile Picture
    7,519 Super User 2025 Season 2 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records

    You're using FindSet(true) which doesn’t lock the table (true parameter = allow write operations while iterating). This means that the record set is not truly static — it’s more like a dynamic pointer through a result set that can still be affected by changes made during the loop. So when you call proc2() and insert a new record that matches your current filter, it’s possible that the new record gets picked up by the loop — because Business Central’s underlying record iteration logic dynamically adjusts to table changes when using FindSet(true).

    I hope below is helpful.

    Thanks, Ramesh

     

    If this was helpful, please check the "Does this answer your question?" box and mark it as verified.

  • Rudylar Profile Picture
    103 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    By using the FindSet() with false parameter, i get the same result (the next record looped in the repeat will be the newly created)...
  • Rudylar Profile Picture
    103 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    I realise that it does not make sense to completely LOCK the FindSet() set of records (if anyway doable), because I still want to make some Modify(). Meaning that new records added to the set of records shall be prohibited, but modification to already listed records shall be accepted.
     
    What is also weird is that i should even end in an infite loop, where a record previously added would be then "cursored/pointed", etc.... But in my case, the repetition only happens once.
  • Verified answer
    Khushbu Rajvi. Profile Picture
    19,307 Super User 2025 Season 2 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    FindSet() does not return a static snapshot of records; it's a live iterator over the filtered set, meaning that if you insert new records matching the filter during the loop (even with FindSet(true)), those records can be included in the iteration. This explains why a newly inserted record appears next in your loop. It doesn't cause an infinite loop in your case because the inserted record doesn’t meet the "Starting Date" = Today condition, so proc2() isn’t called again. To avoid this behavior while still allowing modifications to existing records, a common best practice is to first collect the primary keys of the target records into a list, then iterate over that list and fetch each record individually—this gives you a consistent record set and avoids dynamically including new inserts during the loop.
     
    procedure proc1()
    var
        PurchPrice: Record Table1;
        KeyList: List of [Code[20]]; // assuming primary key includes a field like this
    begin
        PurchPrice.SetRange("Vendor No.", 'V001');
        if PurchPrice.FindSet() then
            repeat
                KeyList.Add(PurchPrice."Primary Key Field"); // replace with your actual key
            until PurchPrice.Next() = 0;
        foreach var Key in KeyList do begin
            if PurchPrice.Get(Key) then
                if PurchPrice."Starting Date" = Today then
                    proc2();
        end;
    end;
     
  • Rudylar Profile Picture
    103 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    Thanks for the best practice. That answers to my question. 
     
    Bonus question: this PurchPrice record's Key is composed by multiple fields with multiple types (code/date/etc). Would you recommend using a x dimension array of Variant, or concatenate on a single Text List, or something else ?

    Thx
  • Rudylar Profile Picture
    103 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    Could I use this trick without altering my Table1 :
     
    var
        PurchPrice: Record Table1;
        PurchPriceTemp: Record Table1 temporary;
    begin
        PurchPrice.SetRange("Vendor No.", 'V001');
        if PurchPrice.FindSet() then
            repeat
                PurchPriceTemp.Insert(after setting every fields)
            until PurchPrice.Next() = 0;
        if PurchPriceTemp.FindSet() then begin
            repeat
                //logic on each record
            until
        end;
    end;
  • Suggested answer
    YUN ZHU Profile Picture
    93,151 Super User 2025 Season 2 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    Hi, It seems to be related to the following question.
     
    And the way you write it, you insert a fixed value each time, you don’t pass in any variables. . . .
     
    Thanks.
    ZHU
  • Suggested answer
    Noha Mbi Profile Picture
    315 on at
    Rec1.Insert() inside a repeat/until loop through FindSet() set of records
    Hello dear, 
    Your code is weak a little bit.
     
       procedure proc1()
        var
            PurchPrice: Record Table1;
        begin
            PurchPrice.Reset();
            PurchPrice.SetRange("Vendor No.", 'V001');
            if PurchPrice.findfirst() then 
                repeat
                    if PurchPrice."Starting Date" = Today then
                        proc2();
                until PurchPrice.Next() = 0;
        end;
     
     
    procedure proc2()
        var
            purchPrice: Record Table1;
        begin
            purchPrice.Init();
            purchPrice.Validate("Item No.", 'itemNo');
            purchPrice."Vendor No." := 'V001';
            purchPrice."Starting Date" := Today + 365;
            purchPrice.Insert();
        end;
     

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 3,750

#2
Sumit Singh Profile Picture

Sumit Singh 2,689

#3
YUN ZHU Profile Picture

YUN ZHU 1,987 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans