# loops

**URL:** <https://forum.servoy.com/t/loops/5639>\
**Category:** Classic Servoy\
**Created:** [March 27, 2006, 6:21am UTC](https://forum.servoy.com/t/loops/5639 "2006-03-27T06:21:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rodneysieb](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@rodneysieb](https://forum.servoy.com/u/rodneysieb)\
**Post date:** [March 27, 2006, 6:21am UTC](https://forum.servoy.com/t/loops/5639/1 "2006-03-27T06:21:43Z")

</div>

i am a fair way through a conversion of a filemaker solution to servoy and so far am finding this reasonably straightforward (event programming is amazing!) and am now starting on converting some of the looping scripts. Javascript loop i am using is:

var new\_date = purchase\_date  
var foundset\_size = purchases\_to\_lineitems.getSize()

for ( var i = 0 ; i \< foundset\_size ; i++ )  
{  
forms.lines\_in.controller.setSelectedIndex = i  
purchases\_to\_lineitems.line\_date = new\_date  
}

i am missing something obvious as the dates are not being set at all. All variables are correct. i am calling this method from a purchase table form through a portal to change the line date of the child records.

Would really appreciate some help.

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [March 27, 2006, 6:30am UTC](https://forum.servoy.com/t/loops/5639/2 "2006-03-27T06:30:05Z")

</div>

Your problem is that getSize() will deliver the fetch size of the relaion, which is 40. Servoy fetches only the fetch size of foundsets, which is 40 for relations and 200 for table foundsets. The rest is loaded “on demand”, usually when you scroll to the end.

In your case it goes wrong, because you ask the foundst for its size before you enter the loop once. In the case of 55 records, this will return 40 and that’s it. To overcome this, change your code to

```auto
var new_date = purchase_date

for ( var i = 0 ; i < purchases_to_lineitems.getSize() ; i++ )
{
var vLineItem = purchases_to_lineitems.getRecord(i);
vLineItem.line_date = new_date;
}

```

In this case, getSize will be updated if 40 is reached. I have also changed the index selection to a more abstract notation (getRecord), which does not update th UI and is faster.

Hope this helps  
Patrick

P.S.: Welcom to Servoy! Event programming will not be the only thing you will find amazing!

---

<div class="post-metadata">

**Author:** ![rodneysieb](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@rodneysieb](https://forum.servoy.com/u/rodneysieb)\
**Post date:** [March 27, 2006, 10:21am UTC](https://forum.servoy.com/t/loops/5639/3 "2006-03-27T10:21:14Z")

</div>

Many thanks for your quick reply.

when i run this method now i get the following error

“cannot convert null to an object”

when i watch the variables your vLineItem = null

I have 6 records in the portal and all refences to the relationship seem right.

regards  
Rodney

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [March 27, 2006, 10:34am UTC](https://forum.servoy.com/t/loops/5639/4 "2006-03-27T10:34:04Z")

</div>

Oops. That was my fault. Usually things in the Java area are zero based, but not foundsets. So change this to:

```auto
var new_date = purchase_date

for ( var i = 1 ; i <= purchases_to_lineitems.getSize() ; i++ )
{
var vLineItem = purchases_to_lineitems.getRecord(i);
vLineItem.line_date = new_date;
}

```

---

<div class="post-metadata">

**Author:** ![rodneysieb](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@rodneysieb](https://forum.servoy.com/u/rodneysieb)\
**Post date:** [March 27, 2006, 11:42am UTC](https://forum.servoy.com/t/loops/5639/5 "2006-03-27T11:42:12Z")

</div>

Again thanks for your quick response.

My method now runs but again only changes the first record and doesn’t loop through all related records. I decided to pare it all back to just see any basic loop work!

Created a table with one text field (new\_text), 7 records, one looping method:

for ( var i = 1 ; i \<= 7 ; i++ )  
{  
new\_text = “anything!!!”;  
}

when i run this script, again only the current selected changes no other record values are set. Starting to get worried! i’m missing something obvious.

regards,  
Rodney

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [March 27, 2006, 12:13pm UTC](https://forum.servoy.com/t/loops/5639/6 "2006-03-27T12:13:19Z")

</div>

Yes, you are. Look closely at my code:

```auto
var vLineItem = purchases_to_lineitems.getRecord(i); 

```

This will load the i-th record into the variable vLineItem. Now with

```auto
vLineItem.line_date = new_date;

```

the property “line\_date” of the variable vLineItem (that contains the i-th record) is set to the contents of your “new\_date” variable.

Your code loops perfectly, but you always set the value to the record the cursor sits on (typically the first record). So seven times you set “new\_text” of the **first** record to “anything!!!”. Change your code to

```auto
for ( var i = 1 ; i <= 7 ; i++ )
{
var iRecord = foundset.getRecord(i); // loads record i into the variable iRecord
iRecord.new_text = "anything!!!"; // sets "anything!!!" into the field "new_text"
}

```

and it works.

I recommend using the debugger to really see what is going on. Open the method editor, click “enable debugger”, choose “enable entry break” from the little triangle next to “enable debugger” and have a look at the variables tab of the debugger window. You should then see the “iRecord” variable getting filled every time with the i-th record while you step through the loop.

Hope this helps,  
Patrick

---

<div class="post-metadata">

**Author:** ![rodneysieb](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@rodneysieb](https://forum.servoy.com/u/rodneysieb)\
**Post date:** [March 28, 2006, 12:21am UTC](https://forum.servoy.com/t/loops/5639/7 "2006-03-28T00:21:23Z")

</div>

THIS IS THE BEST TECHNICAL SUPPORT I’VE EVER HAD!

Thanks mate

Everything is working fine now, your explanation really helps. Loops in servoy are very different to filemaker and are the first thing thats taken awhile to get my head around. Now that i’ve got my first loop working should be ok from now on.

one last question: can you confirm that the “.getSize ()” fx will work with foundsets greater than the 200 limit i keep reading about.

Again thanks so much

---

<div class="post-metadata">

**Author:** ![patrick](https://avatars.discourse-cdn.com/v4/letter/p/b2d939/32.png) [@patrick](https://forum.servoy.com/u/patrick)\
**Post date:** [March 28, 2006, 5:51am UTC](https://forum.servoy.com/t/loops/5639/8 "2006-03-28T05:51:36Z")

</div>

Yes, getSize() on a foundset directly (not related) will work the same. But do not put getSize() into a variable and loop using that variable. Then you “freeze” at 200. Instead use

```auto
for ( var i = 1 ; i <= foundset.getSize() ; i++ )
{
var record = foundset.getRecord(i);
}

```

If you reach 200, getSize will automatically increase to 400, 600, 800 etc. depending on the actual size of the foundset. What you can also do is

```auto
var foundsetSize = databaseManager.getFoundSetCount(foundset); // returns the actual size of the foundset, can be timeconsuming if you have a large foundset
for ( var i = 1 ; i <= foundsetSize ; i++ )
{
var record = foundset.getRecord(i);
}

```

this way you “freeze” the real size, not the fetch size.
