# Aight, I give up. What'm I doin' wrong?

**URL:** <https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517>\
**Category:** Classic Servoy\
**Created:** [January 22, 2005, 12:32am UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517 "2005-01-22T00:32:21Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 12:32am UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/1 "2005-01-22T00:32:21Z")

</div>

I have a field that I need to update in all records. My client has about 1500 records. I have about 50 test records. The method below works in mine, but not his. On his system, java eats RAM like popcorn, but never finishes the job.

I assume it has something to do with loading 200 rows at a time, but at this point, I’m just not sure. I’m betting one of the geniuses here can just look at the code, snicker, and tell me where I’m screwing up.

TIA

```auto
forms.leadsRec.controller.show();
forms.leadsRec.controller.loadAllRecords();
while ( forms.leadsRec.controller.getSelectedIndex() < forms.leadsRec.controller.getMaxRecordIndex())
{
	forms.leadsRec.controller.setSelectedIndex (forms.leadsRec.controller.getMaxRecordIndex());
}
for (i=forms.leadsRec.controller.getMaxRecordIndex();i>0 ;i--)
{
	forms.leadsRec.controller.setSelectedIndex(i);
	if ( databaseManager.hasRecords(forms.leadsRec.leads_to_media_outlet_by_name))
	{
		forms.leadsRec.k_media_outlet = forms.leadsRec.leads_to_media_outlet_by_name.pk;
	}
}

```

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 22, 2005, 8:42am UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/2 "2005-01-22T08:42:13Z")

</div>

Maybe this works:

(btw what are you trying to do here?)

```auto
forms.leadsRec.controller.show(); 
forms.leadsRec.controller.loadAllRecords();

for ( i=i ; i <= forms.leadsRec.controller.getMaxRecordIndex() ; i++ ) { 
 var record = forms.leadsRec.foundset.getRecord(i);

 if ( databaseManager.hasRecords(record.leads_to_media_outlet_by_name)) {
  record.k_media_outlet = record.leads_to_media_outlet_by_name.pk; 
 } 
}

```

---

<div class="post-metadata">

**Author:** ![automazione](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@automazione](https://forum.servoy.com/u/automazione)\
**Post date:** [January 22, 2005, 9:46am UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/3 "2005-01-22T09:46:01Z")

</div>

> Cain:  
> I assume it has something to do with loading 200 rows at a time, but at this point, I’m just not sure.

You’re right… I think your method processes only the records that are in your foundset (200 unless you already loaded a bigger foundset …) Go the way IT2BE is telling you and getMaxRecordIndex will keep authomatically increaseing so you will process all your records. If for some reason you need to process the records “bottom-up” you can place a sort before the loop…

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 4:14pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/4 "2005-01-22T16:14:49Z")

</div>

> (btw what are you trying to do here?)

I have a field that should’ve been a “lookup”. The user chooses a name, and I need to capture the actual related key so that if the name changes, the link isn’t broken. I’m just trying to go back and update existing records.

The alternative you describe is, I believe, what I started with. ![:(]( "Sad") It _appeared_ to process the first 200 records and get stuck. That’s why I added the while loop, to try to ensure that all the records were loaded. I’m only working from back to front because, at the end of the while loop, I expect to be on the last record, anyway.

I’ll try what you’ve posted, anyway, though. Perhaps it’s different in some respect that I don’t immediately see.

I’m trying to get the production data loaded into my local copy, but I’m having a separate problem importing the sample data.

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 22, 2005, 4:27pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/5 "2005-01-22T16:27:53Z")

</div>

Nope, I can’t believe it is the same. Maybe you did a check on ‘i \< controller.getMaxRecordIndex()’.

When you do that the loop stops **below** that last indexed record. When you do ‘i \<= controller.getMaxRecordIndex()’ the loop goes on because at 200 records (or a multitude) servoy will load the next 200 records and thus ‘controller.getMaxRecordIndex()’ is now 400…

I hope I am clear enough on this with my broken english ![:-)]( "Smile")

Ciao

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 4:30pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/6 "2005-01-22T16:30:31Z")

</div>

AHA! I’ll be that was it!

Excellent explanation. I’ll give it a shot now.

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 5:57pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/7 "2005-01-22T17:57:38Z")

</div>

That did it. Thanx much!

That also bent my mind a bit. At first, I thought you’d forgotten to setSelectedIndex. I hadn’t realized you could load a record into a variable like that. Wow!

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 6:00pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/8 "2005-01-22T18:00:30Z")

</div>

OK, so now I find that in order to get a stored calc to update, I need to step through existing records.

First off, as I understand it, I don’t have to show the field on the form, just select that record, right?

Second, I’m trying to use the code below, and I’m getting the same sort of “endless loop” type behavior. Perhaps I’m doing the same thing wrong again here?

It’s frustrating, because with my smaller record counts, this works fine.

```auto
forms.callsRec.controller.show();
for ( i=1 ; i <= forms.callsRec.controller.getMaxRecordIndex() ; i++ )
{
   currentcontroller.setSelectedIndex(i);
}

```

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 22, 2005, 6:04pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/9 "2005-01-22T18:04:25Z")

</div>

I wouldn’t do it like you do. Instead of:

```auto
forms.callsRec.controller.show(); 
for ( i=1 ; i <= forms.callsRec.controller.getMaxRecordIndex() ; i++ ) 
{ 
 currentcontroller.setSelectedIndex(i); //why do you go to currentcontroller????
}

```

I would do

```auto
forms.callsRec.controller.show(); 
for ( i=1 ; i <= forms.callsRec.controller.getMaxRecordIndex() ; i++ ) 
{ 
 forms.callsRec.controller.setSelectedIndex(i); 
}

```

BTW you also don’t have to do a controller.show();…

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 6:20pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/10 "2005-01-22T18:20:30Z")

</div>

OK, I just do the show so that I can see the form on screen.

Currentcontroller vs. a named form doesn’t account for why this doesn’t work, does it?

Incidentally, why would you name a form when it’s the current controller? Since Servoy doesn’t update names across methods, this just seems like one more place to have to change if the form name ever changes.

Programming styles aside, though, (and I do like your style) I don’t think this explains why this just flat out doesn’t work on my client’s data. I just ran it for over 20 minutes, with only 1300 records, and it hadn’t redrawn the screen.

I just need a stored calc to re-evaluate. This must happen to others, and frequently. Isn’t there just a simple “step through and update” routine floating around somewhere?

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 22, 2005, 6:27pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/11 "2005-01-22T18:27:27Z")

</div>

> Cain:  
> OK, I just do the show so that I can see the form on screen.
> 
> Currentcontroller vs. a named form doesn’t account for why this doesn’t work, does it?
> 
> Incidentally, why would you name a form when it’s the current controller? Since Servoy doesn’t update names across methods, this just seems like one more place to have to change if the form name ever changes.

Simply because the currentcontroller points to the controller of the MAIN form. So if this is a form in a tabpanel the currentcontroller is not the controller you wish to work with…

> Programming styles aside, though, (and I do like your style) I don’t think this explains why this just flat out doesn’t work on my client’s data. I just ran it for over 20 minutes, with only 1300 records, and it hadn’t redrawn the screen.
> 
> I just need a stored calc to re-evaluate. This must happen to others, and frequently. Isn’t there just a simple “step through and update” routine floating around somewhere?

Hmm, this time I don’t have an answer but maybe to try to do a controller.loadAllRecords() first…

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 7:45pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/12 "2005-01-22T19:45:10Z")

</div>

loadAllRecords didn’t seem to help. ![:(]( "Sad")

Anyone have any other ideas why this doesn’t work?

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 22, 2005, 7:49pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/13 "2005-01-22T19:49:51Z")

</div>

I notice this only now:

> OK, so now I find that in order to get a stored calc to update, I need to step through existing records.

Why do you need to step through the records to update the stored calculation?

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 22, 2005, 7:56pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/14 "2005-01-22T19:56:03Z")

</div>

What other method can I use?

I have a stored calculation that I just defined. Problem is, for the 1300 existing records, this calc hasn’t been evauated. Until I ‘look’ at the records, I won’t get the right value.

Or am I completely off base here?

I have a new report that uses this stored calc field for a subsummary. As it stands now, only the value for the first record figures, and all records show under one subsummary.

As a test, I manually stepped through a day’s worth of records (about 50). For that day, and that day alone, I can get an accurate report, with subsummaries for all the different resulting values.

If this still isn’t making sense, I could send you the solution back channel. It might be easier to understand if you could see it.

Thanks so much for taking the time, Marcel.

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 22, 2005, 8:03pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/15 "2005-01-22T20:03:54Z")

</div>

So, if I undestand correct you have a production system. And, you created a new stored calc for one of the tables and now want to update that field?

To be honest I haven’t done that but what you try to do seems a little drastic. Won’t a controller.saveRecord() (only once) do the job?

At least I’m pretty sure there is another way you can do that!

I this doesn’t help that solution backchannel is a good idea…

---

<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:** [January 22, 2005, 8:41pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/16 "2005-01-22T20:41:12Z")

</div>

Why don’t you simply walk through all your records like IT2Be suggested and do something like

```auto
for ( i=1 ; i <= forms.callsRec.controller.getMaxRecordIndex() ; i++ ) 
{
   var record = getRecord(i);
   record.k_media_outlet;
}

```

That should calculate everything as you specified in the calculation. There is a problem updating calcs right now (there are quite a few discussions on this going on), but this way everything should get calculated.

---

<div class="post-metadata">

**Author:** ![automazione](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@automazione](https://forum.servoy.com/u/automazione)\
**Post date:** [January 22, 2005, 8:46pm UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/17 "2005-01-22T20:46:56Z")

</div>

When you add a new stored field on an already populated table you must loop through all your records to invoke the calculation and store the value. Or still better it would be to run an sql update command directly on your db.

---

<div class="post-metadata">

**Author:** ![Cain](https://avatars.discourse-cdn.com/v4/letter/c/85e7bf/32.png) [@Cain](https://forum.servoy.com/u/Cain)\
**Post date:** [January 24, 2005, 1:18am UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/18 "2005-01-24T01:18:48Z")

</div>

Thanks, Patrick! That seems to have done it.

Still not sure why some of of the other methods didn’t work, but as the mission is accomplished, and I have this handy routine to mod and use in the future, I’m ok now. ![:-)]( "Smile")

---

<div class="post-metadata">

**Author:** ![IT2Be](https://avatars.discourse-cdn.com/v4/letter/i/dc4da7/32.png) [@IT2Be](https://forum.servoy.com/u/IT2Be)\
**Post date:** [January 24, 2005, 6:48am UTC](https://forum.servoy.com/t/aight-i-give-up-whatm-i-doin-wrong/3517/19 "2005-01-24T06:48:09Z")

</div>

So you’re happy now? That’s good news since my evaluation didn’t come to table yesterday (sorry)…
