# Trigger global method from calculation

**URL:** <https://forum.servoy.com/t/trigger-global-method-from-calculation/2986>\
**Category:** Classic Servoy\
**Created:** [October 30, 2004, 1:28pm UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986 "2004-10-30T13:28:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [October 30, 2004, 1:28pm UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986/1 "2004-10-30T13:28:48Z")

</div>

I gather this can be done but am having trouble getting it to work! If I simply add globals.myMethod() to a calculation should it trigger the method? If so, can I get a value from the method back into the calculation? i.e. can I run the method, set a global variable with a result and then use that variable in the next steps of the calculation?

I would really like to be able to run an sql select as part of a global method (triggered from a calculation) in order to return a value to a dataprovider without having to resort to relationships.

Any help gratefully received!

---

<div class="post-metadata">

**Author:** ![dbonnett](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@dbonnett](https://forum.servoy.com/u/dbonnett)\
**Post date:** [October 30, 2004, 10:49pm UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986/2 "2004-10-30T22:49:55Z")

</div>

> drookwood:  
> I gather this can be done but am having trouble getting it to work! If I simply add globals.myMethod() to a calculation should it trigger the method? If so, can I get a value from the method back into the calculation? i.e. can I run the method, set a global variable with a result and then use that variable in the next steps of the calculation?
> 
> I would really like to be able to run an sql select as part of a global method (triggered from a calculation) in order to return a value to a dataprovider without having to resort to relationships.
> 
> Any help gratefully received!

I use this as follows:  
In a global called Duration, I have:

```auto
return globals.Days_Between(due_diligence_,date_entered);

```

My method (Days\_Between) then looks like (simplified..):

```auto
var date1= arguments[0], date2=arguments[1]; {
if (!date1) { return -999};
{code here}
    return Math.round(difference_ms/ONE_DAY)

```

Both the calculation and the method need to return a value for every outcome, but other than that, you can do almost anything (I would be  
careful about changing other values in the record, but that is a guess, the guys from Servoy may be able to tell you for certain)

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [October 31, 2004, 7:58am UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986/3 "2004-10-31T07:58:52Z")

</div>

that should work fine.

You shouldn’t do time consuming stuff like a sql query in a calculation. A calculation should be as quick as possible.

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [October 31, 2004, 11:53am UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986/4 "2004-10-31T11:53:22Z")

</div>

dbonnet: thanks - it was the use of ‘return’ in the method that I wasn’t getting. Now works OK.

jcompagner: what if the sql is simply to get a single value? for example: I need to get a value into a dataprovider from a related record. Is it always better to establish a relationship, even if only for a single item rather than embed a simple sql select in a calculation. (I thought I read in one post that keeping down the number of relationships is a good idea with regard to solution speed and loading times, so I was looking for alternatives where a relationship is only required for one item).

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [October 31, 2004, 12:42pm UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986/5 "2004-10-31T12:42:28Z")

</div>

an embedded sql will always really do a query to the database.  
And you have no control over when a calculation is calculated. So this will be expenisve.

A relation will be much faster because we can handle the db access and cache when we can.

---

<div class="post-metadata">

**Author:** ![drookwood](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@drookwood](https://forum.servoy.com/u/drookwood)\
**Post date:** [October 31, 2004, 3:35pm UTC](https://forum.servoy.com/t/trigger-global-method-from-calculation/2986/6 "2004-10-31T15:35:23Z")

</div>

Thanks
