Need help with foundset.getRecord()

Version: 7.4.4 - build 2039

Would someone be so kind as to critique the below code? I am attempting to iterate over a foundset and add new records to the same foundset…but, it’s not working, and I cannot figure out why. The first time through the loop, CD_Census is loaded with the correct record data. On subsequent loop iterations, CD_Census record data does not change (it’s always the data from the first record) even through loop iterator i is changing (verified by ci).

If the last 3 lines of code are commented out, then CD_Census record data changes along with the loop iterator.

While diagnosing this behavior, and when not commented out, I discovered that the 3rd line from the bottom (var index_census_new = fs1.newRecord( false, false )) always returns a 1, even though the foundset count is 339.

Am I doing something incorrectly? Help, please!

var recordCount = databaseManager.getFoundSetCount( fs1 )		// recordCount = 339

for ( var i = 1; i <= recordCount; i++ )
{
    /** @type {JSRecord<db:/arm_data/billing_census>} */
    
    var CD_Census = fs1.getRecord( i )     // i = 1, ci = 1, data from record index 1 loaded
                                           // i = 2, ci = 2, data from record index 2 not loaded, still has data from record index 1
                                           // i = 3, ci = 3, data from record index 3 not loaded, still has data from record index 1
                                           // ad nauseum
																
    var ci = fs1.getRecordIndex( CD_Census )					// ci tracks with iterator i (just making sure the index is incrementing)


	
    var index = CD_Census.census_to_census_summary.getSelectedIndex()
    
    /** @type {JSRecord<db:/arm_data/billing_census_summary>} */
    
    var CD_Summary = CD_Census.census_to_census_summary.getRecord( index )	
    
    
    
    var index_census_new = fs1.newRecord( false, false )    	// <<<<<--- newRecord() always returns 1 (should be > recordCount).
    
    /** @type {JSRecord<db:/arm_data/billing_census>} */
    
    var CD_Census_New = fs1.getRecord( index_census_new )

}

Hi Kim,
the problem is this: var recordCount = databaseManager.getFoundSetCount( fs1 ) // recordCount = 339

When looping over a foundset you should use foundset.getSize() because Servoy never loads the full foundset, it usually loads records in batches of 200 records and loads more records as needed.

Try changing your loop like this:

for ( var i = 1; i <= fs1.getSize(); i++ )

This will force Servoy to reevaluate the foundset size at every iteration (and when it reaches the 200 recs it will trigger a load of a new batch of recs from the db).

i am not sure why this:

var index_census_new = fs1.newRecord( false, false ))

always returns a 1

but it doesn’t have to return 339 or something higher
If not all the records are loaded then newRecord will be for example on position 200
so if you do newRecord(false,false) it should return the value getSize() + 1

But it seems to that you encountered a bug that the addToTop boolean that is false still adds it somehow to the top.
If you can reproduce this in an example then make a case

Hi, Nicola.

Thank you for responding!

I tried your suggestion, but it did not make any difference in the outcome. I also tried hard-coding index values, but that did not work as well.

Case submitted - SVY-8322

Hi Kim,
I see that your problem is something else but my suggestion is still valid if you loop a foundset greater than 200 recs (or 60 recs if it’s a related foundset), keep it in mind.

i checked out the sample solution and i want to reply also here to teach people what shouldn’t be done

If you have a foundset where you search for something and then iterate over, don’t or try to avoid altering the foundset again by doing inserts on that foundset where you iterate over
Because this will adjust the iteration (an insert to the top will end up that the next index will point to the same record as before because that one moves up)
Also appending the new record to the bottom only works if the foundset is fully loaded (we always now append to the top if the foundset is not fully loaded, else it will be somewhere in the middle and you would hit this new record again when you iterate)

Always split this up into 2 foundset, 1 that you search and iterate over and 1 where you do the inserts on.

Deletes should be fine but do remember that you then have to adjust the iteration if you iterate by index (the index should be minus 1 of you delete)

Ah, now I understand what is happening! Johan, this, along with your support response (screenshot below), is a great explanation. I was unaware that records are always being inserted at the top of the foundset regardless of the newRecord() changeSelection parameter setting for partially loaded foundsets. Others may be unaware of this as well. I strongly encourage you to put this information in the wiki as it is a rather important concept. Thank you so much for the help!

[attachment=1]svy_8322.png[/attachment]

[attachment=0]wiki_newRecord.png[/attachment]