Page 1 of 1

Calculate a field value by reference?

PostPosted: Thu Feb 06, 2020 3:31 am
by dponti
It seems that this should be simple to do, but I am too Javascript challenged to figure this out.

I want to construct a calculated field to show on a list form whose value is the value of another dataprovider in the same table where the dataprovider name is provided in a global field, rather than hard-coding the dataprovider name in the calculation method. Obviously returning the global field (eg. return scopes.globals.fieldName) doesn't work. I gather that i need to return an object but not sure how to construct it? I also tried writing a form method that returns the value of getDataProviderValue() to the calculation, but this only gives the value of the first record of the foundset, rather than the value for each individual record as would be the case if the dataprovider was hardcoded into the method.

Any idea how (or whether) I can do this? Thanks!

Re: Calculate a field value by reference?

PostPosted: Thu Feb 06, 2020 11:43 am
by ROCLASI
Hi,

You could use the following code in your calculation:
Code: Select all
return foundset[scopes.globals.fieldName];


Hope this helps.

Re: Calculate a field value by reference?

PostPosted: Thu Feb 06, 2020 8:47 pm
by dponti
Hi -

Thanks for your reply and suggestion, but it doesn't seem to work. What is returned from the calculation is the value of "fieldName" for the first record in the foundset for all of the records, instead of the specific value of "fieldName" for each record. This is the same result I got when trying to pass a form method's dataProviderValue to the calculation - it doesn't recalculate for each row, Your code also throws the following warning: "Reference to undeclared variable or property foundset", so apparently foundset is not understood in the context of a table calculation. Any other suggestions?

Re: Calculate a field value by reference?

PostPosted: Tue Mar 17, 2020 5:09 pm
by Joas
"foundset" references the entire foundset, but not a specific record, so that's why you always see the value of the first (or maybe selected?) record.

Instead you should use:
Code: Select all
return this[scopes.globals.fieldName];

"this" references the record that the calculation is executed for.