Calculate a field value by reference?

Questions and answers for designing and implementing forms in Servoy

Calculate a field value by reference?

Postby dponti » Thu Feb 06, 2020 3:31 am

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!
dponti
 
Posts: 70
Joined: Wed Apr 14, 2004 9:39 pm
Location: Menlo Park, CA

Re: Calculate a field value by reference?

Postby ROCLASI » Thu Feb 06, 2020 11:43 am

Hi,

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


Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Twitter: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5342
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: Calculate a field value by reference?

Postby dponti » Thu Feb 06, 2020 8:47 pm

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?
dponti
 
Posts: 70
Joined: Wed Apr 14, 2004 9:39 pm
Location: Menlo Park, CA

Re: Calculate a field value by reference?

Postby Joas » Tue Mar 17, 2020 5:09 pm

"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.
Joas de Haan
Yield Software Development: Need help on your project?
yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 813
Joined: Mon Mar 20, 2006 4:07 pm
Location: Amersfoort, NL


Return to Forms

Who is online

Users browsing this forum: No registered users and 4 guests