# Calculate a field value by reference?

**URL:** <https://forum.servoy.com/t/calculate-a-field-value-by-reference/20183>\
**Category:** Questions\
**Created:** [February 6, 2020, 1:31am UTC](https://forum.servoy.com/t/calculate-a-field-value-by-reference/20183 "2020-02-06T01:31:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dponti](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@dponti](https://forum.servoy.com/u/dponti)\
**Post date:** [February 6, 2020, 1:31am UTC](https://forum.servoy.com/t/calculate-a-field-value-by-reference/20183/1 "2020-02-06T01:31:42Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [February 6, 2020, 9:43am UTC](https://forum.servoy.com/t/calculate-a-field-value-by-reference/20183/2 "2020-02-06T09:43:38Z")

</div>

Hi,

You could use the following code in your calculation:

```auto
return foundset[scopes.globals.fieldName];

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![dponti](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@dponti](https://forum.servoy.com/u/dponti)\
**Post date:** [February 6, 2020, 6:47pm UTC](https://forum.servoy.com/t/calculate-a-field-value-by-reference/20183/3 "2020-02-06T18:47:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)\
**Post date:** [March 17, 2020, 3:09pm UTC](https://forum.servoy.com/t/calculate-a-field-value-by-reference/20183/4 "2020-03-17T15:09:36Z")

</div>

“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:

```auto
return this[scopes.globals.fieldName];

```

“this” references the record that the calculation is executed for.
