Can Not Update Data with Methods

OK:

My frustration level is going up. I’ve tried numerous method tests to try to update my records with a “script/method”. I can edit the records manually, no problem. None of my attempts to update programmatically have worked. Here’s my last test:

Clarification: THe field here is being updated, but it’s not causing the calculated fields to be recalculated (which is what I’m really trying to do).

Now, the debugger is showing the proper value, but when I go back to look at the records, the field is not displaying the value.

for ( var i = 0 ; i <=
controller.getMaxRecordIndex() ; i++ )
{
controller.recordIndex = i;
forms.payments.old_payment_recnum.enabled = true;
forms.payments.old_payment_recnum = 1;
}
buttonresult =
plugins.dialogs.showInputDialog(“Question,What day is it today?”);

The records loop, the message displays, but my field is never updated.

What the heck am I doing wrong?

Thanks
Lee Snover

Are ou sure you are looping through the foundset you want to update?

leesnover:
for ( var i = 0 ; i <=
controller.getMaxRecordIndex() ; i++ )
{
controller.recordIndex = i;
forms.payments.old_payment_recnum = 1;

controller.recordIndex is the controller on the active form… the variable you are updating is on payments form …looks strange to me, seems you are not looping on payment form … or what was the objective of your method?

btw: controller.recordIndex is obsolete, better use the function controller.setSelectedIndex(i)

Hi Lee

Stick with it - the end result is worth the initial pain :)

Not sure if this will solve your problem but Johan Compagner posted this suggestion in the thread: http://www.forum.servoy.com/viewtopic.php?t=3685

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

HTH

Regards

Graham Greensall
Worxinfo Ltd

grahamg:
Hi Lee

Stick with it - the end result is worth the initial pain :)

Not sure if this will solve your problem but Johan Compagner posted this suggestion in the thread: http://www.forum.servoy.com/viewtopic.php?t=3685

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

HTH

Regards

Graham Greensall
Worxinfo Ltd

Graham:

I tried this. “record” caused a syntax error. I tried:

forms.payments.mycalcfield;

But this did not do anything. I’m missing some basic concepts here, which I’m sure is part of the problem.

THanks,
Lee

Hello Lee,

probably the easiest is if you tell us

  • how your table looks like
  • what form you are using (what table is it based on)
  • how the calculation looks like and how it is called

It does look like you are missing something minor probably. Everybody works with getRecord() and everybody has calcs that work. So if you tell us more, I am sure we can help you out. If you write “record caused a syntax record” it sounds weird, because nothing seems wrong in your code. So either you have no foundset, your form is based on a different table or something else…

patrick:
Hello Lee,

probably the easiest is if you tell us

  • how your table looks like
  • what form you are using (what table is it based on)
  • how the calculation looks like and how it is called

It does look like you are missing something minor probably. Everybody works with getRecord() and everybody has calcs that work. So if you tell us more, I am sure we can help you out. If you write “record caused a syntax record” it sounds weird, because nothing seems wrong in your code. So either you have no foundset, your form is based on a different table or something else…

Patrick:

This is a simple table. Basic check records. I have a field that is has a full two part description of the Payment Type, payment_type_full, ie. “Medical:Hospital”, I have a Calculated field that breaks this up into it’s two pieces for Report summarization: payment_type_c and payment_subtype_c. Which are calculated values. Here’s the calculation for the payment_type_c field:

if(payment_type_full)
{
return utils.stringMiddle(payment_type_full, 1, utils.stringPosition(payment_type_full, ‘:’,1, 1)-1);
}
else
{
return ‘NOT VALID’;
}

If I MANUALLY edit Payment_Type_Full, the value in payment_type_c updates. I have not been able to get it to update any other way. Here’s the last code I tried:

for ( var i = 0 ; i <=
controller.getMaxRecordIndex() ; i++ )
{
controller.recordIndex = i;
forms.payments.payment_type_c;
}
buttonresult =
plugins.dialogs.showInputDialog(“test”);

It scrolls through the records, and shows me the dialog, but nothing else happens. Here’s my other shot:

var temp = “”;
for ( var i = 0 ; i <=
controller.getMaxRecordIndex() ; i++ )
{
controller.recordIndex = i;
forms.payments.old_payment_recnum.enabled = true;
forms.payments.old_payment_recnum = 1;
temp = forms.payments.payment_type_full;
forms.payment_type_full=‘NA’;
forms.payment_type_full = temp;
forms.payments.payment_type_c=utils.stringMiddle(payment_type_full, 1, utils.stringPosition(payment_type_full, ‘:’,1, 1)-1);
forms.payments.controller.saveData();
}
buttonresult =
plugins.dialogs.showInputDialog(“test”);

This errors on the line where I try to set the value to ‘NA’ with this error:
“Can’t put in a locked scope, name: payment_type_full, value: NA”

I don’t know why this error is occurring or what it means.

Not sure if that helps, but that’s what I’ve been trying.

Regards,
Lee

patrick:
Hello Lee,

probably the easiest is if you tell us

  • how your table looks like
  • what form you are using (what table is it based on)
  • how the calculation looks like and how it is called

It does look like you are missing something minor probably. Everybody works with getRecord() and everybody has calcs that work. So if you tell us more, I am sure we can help you out. If you write “record caused a syntax record” it sounds weird, because nothing seems wrong in your code. So either you have no foundset, your form is based on a different table or something else…

Patrick:

I tried declaring a record type and now I’m getting a different error. And now editing the field is not causing the calc to update either. Here’s the last go round:

for ( var i = 0 ; i <=
controller.getMaxRecordIndex() ; i++ )
{
controller.recordIndex = i;
var record = foundset.getRecord(i);
record.payment_type_c;
}
buttonresult =
plugins.dialogs.showInputDialog(“test?”);

I’m getting an error on the var record = foundset.getRecord(i); line:

“Cannot convert null to an object.”

I fear I’m lost at the moment.

Regards,
Lee

Lee

I don’t know what you are doing but the loop must go this:

for ( var i = 1 ; i <= foundset.getSize() ; i++ )
{
var record = foundset.getRecord(i);
record.payment_type_c; //what are you doing here????? nothing??
}
buttonresult = plugins.dialogs.showInputDialog("test?");

You also doing strange stuff up there!
You scroll through the main form, and your updating records on a complete different form!!

Lee

Following code works for me -

var MaxRec = controller.getMaxRecordIndex()

for ( var i = 0 ; i < MaxRec ; i++ )
{
controller.setSelectedIndex(i);
// get data or set data on fields in this record
}

Have you watched the values of etc in the debugger - this may give you a clue. I’ve seen the ‘Cannot convert null …’ error message but can’t remember what it was about - however think that you are trying to ref something that doesn’t exist. What number shows in your getMaxRecordIndex - I set it to a variable so that its easy to see in debugger.
Keep shouting if it doesn’t work
Graham Greensall
worxinfo Ltd

Sorry, graham I must correct you.

  • var i has to start with 1, you can’t set an selectedIndex to NULL
  • the controller.getMaxRecordIndex (or now: foundset.getSize) has to be inside the loop, or else it won’t loop further dan 200 records.

Harjo

I’m happy to be corrected by one of the real gurus :)

However just ran code with debugger on and Servoy is very forgiving as it just stays on record one for two loops - 0 & 1 - then moves through the rest. But see the dangers of this & the 200 limit so appreciate your advice.

Thanks

Graham Greensall
Worxinfo Ltd

grahamg:
Harjo

I’m happy to be corrected by one of the real gurus :)

However just ran code with debugger on and Servoy is very forgiving as it just stays on record one for two loops - 0 & 1 - then moves through the rest. But see the dangers of this & the 200 limit so appreciate your advice.

Thanks

Graham Greensall
Worxinfo Ltd

OK Guys:

You where right, i was not incrementing the first go round. I now have this:

currentcontroller.recordIndex=1;

for ( var i = 0 ; i <=currentcontroller.getMaxRecordIndex() ; i++ )
{
i++;
controller.recordIndex = i;
var record = foundset.getRecord(i);
record.payment_type_c;
}
buttonresult = plugins.dialogs.showInputDialog(“test?”);

No errors, but nothing changes either. Is the statement
record.payment_type_c; supposed to cause that calculated field ?

Thanks,
Lee Snover

That does not make sense at all!

Please look at my prior posting.

You are doing nothing here:

record.payment_type_c;

HJK:
That does not make sense at all!

Please look at my prior posting.

You are doing nothing here:

record.payment_type_c;

Harjo:

Sorry, I got that from another example. My thought was since this is a calculated field, this command would basically cause the field calculation to run.

All I want to do is get the Stored Calc to re-evaluate on each record in the found set.

I changed to this:
currentcontroller.recordIndex=1;
var temp = ‘NA’

for ( var i = 0 ; i <=currentcontroller.getMaxRecordIndex() ; i++ )
{
i++;
controller.recordIndex = i;
var record = foundset.getRecord(i);
record.payment_type_c = utils.stringMiddle(record.payment_type_full,1,3);
temp = utils.stringMiddle(record.payment_type_full,1,3);
}
buttonresult = plugins.dialogs.showInputDialog(“test?”);

Now, I can confirm that record.payment_type_c is being set and
temp is being set, however, since Record is a declared variable, is it “bound” to the actual record? When I’m finished executing, the actual record values have not changed.

So I can successfully set variables, but I have not succeeded in getting the actual “Record” to update or revalue it’s Stored Calculated fields.

Thank you both for your patience with me. I’ve been programming 20 years and feel like I’m back in first grade at the moment. :-(

Regards,
Lee Snover

Lee

Welcome to the club - I’ve had a lot of ‘first grade’ moments while learning Servoy.

As you’re trying to get the stored calcs to update & current coding not working maybe it’s worth getting the big hammer out.

How about following psuedo-code.

//start loop

var TempStore = record.payment_type_c value (or correct field)
record.payment_type_c = null
savedata

record.payment_type_c = TempStore
savedata

// end loop

Test with a small foundset as it’s a crude approach that probably has more steps than really required but this should force your record & calcs to update twice.

Best of luck - I’m off to bed …

grahamg:
Lee

Welcome to the club - I’ve had a lot of ‘first grade’ moments while learning Servoy.

As you’re trying to get the stored calcs to update & current coding not working maybe it’s worth getting the big hammer out.

How about following psuedo-code.

//start loop

var TempStore = record.payment_type_c value (or correct field)
record.payment_type_c = null
savedata

record.payment_type_c = TempStore
savedata

// end loop

Test with a small foundset as it’s a crude approach that probably has more steps than really required but this should force your record & calcs to update twice.

Best of luck - I’m off to bed …

Thanks for the reply. I don’t even understand why it’s LEGAL to attempt to update a stored Calc. Shouldn’t Servoy prevent this? Also, people keep referring to “record” in their examples. Is this a Servoy object set like “forms”, or a declared variable, or just a place holder for my table name?

Regards,
Lee

Lee

I’m not suggesting you write directly into the Stored Calc.

I was working on basis that [record.payment_type_c] was one of the fields that was used by your Stored Calcs.

I refer to record as if it were a row in a spreadsheet - ie one of the lines in your foundset. But then I’m not a classically trained programmer.

Regards
Graham

grahamg:
Lee

I’m not suggesting you write directly into the Stored Calc.

I was working on basis that [record.payment_type_c] was one of the fields that was used by your Stored Calcs.

I refer to record as if it were a row in a spreadsheet - ie one of the lines in your foundset. But then I’m not a classically trained programmer.

Regards
Graham

I’m not “classicly trained” either. It’s just there is so much “inferred” in modern languages, it’s easy to make incorrect assumptions. I’ll do some more experimenting tomorrow. One of the Servoy folks is supposed to give me a call. Once I get the basic concepts down, I think I’ll be OK.

Thank you again. Off to bed with ya now. ;-)

Regards,
Lee

Suggestion – don’t do this with a loop. Try the following:

Use the foundsetUpdater to update the value in a field across the found set:

var fsUpdater = databaseManager.getFoundSetUpdater(form_name);
fsUpdater.setColumn('field_name', new_field_value);
fsUpdater.performUpdate();

Then force the calculations to update with:

databaseManager.recalculate(foundset);

With calcs–based on calcs–based on calcs across multiple tables this will work where no amount of looping will.

david:
Suggestion – don’t do this with a loop. Try the following:

Use the foundsetUpdater to update the value in a field across the found set:

var fsUpdater = databaseManager.getFoundSetUpdater(form_name);

fsUpdater.setColumn(‘field_name’, new_field_value);
fsUpdater.performUpdate();




Then force the calculations to update with:

databaseManager.recalculate(foundset);


With calcs--based on calcs--based on calcs across multiple tables this will work where no amount of looping will.

David:

I’ll try it, but I haven’t had much luck. All of the “parent field” values are set. The calculated field seems to come and go, I can watch it update when I play with the field it’s dependent on, but when I come back to the record, it’s back to a null value.

This entire issue has been very frustrating.

Thanks!
Lee