Hints and Best Practices

Formulas Evaluation Optimization

Work-Relay application have an optimization feature that works on formulas: if same formula is used several times on the page, it will be calculated once and then re-used.

This feature makes better performance, but in some situations this may cause a trouble. For example, you need to create a set of variables with random values with a Global Action:

If use RANDOM() function, same formula will return the same value. The following code:

{
"var1":"FORMULA[RANDOM()]",
"var2":"FORMULA[RANDOM()]",
"var3":"FORMULA[RANDOM()]",
"var4":"FORMULA[RANDOM()]",
"var5":"FORMULA[RANDOM()]"
}

will actually return result like this, and all variables will have the same value:

{
"var1":"1206324011544567911",
"var2":"1206324011544567911",
"var3":"1206324011544567911",
"var4":"1206324011544567911",
"var5":"1206324011544567911"
}

The first way to avoid this is make each formula unique. This can be simply done by adding unique arguments to function (RANDOM() function ignores arguments). This code:

{
"var1":"FORMULA[RANDOM()]",
"var2":"FORMULA[RANDOM(1)]",
"var3":"FORMULA[RANDOM(2)]",
"var4":"FORMULA[RANDOM(a)]",
"var5":"FORMULA[RANDOM('qwerty')]"
}

will return different results as expected:

{
"var1":"2506444008547775918",
"var2":"-4272344904841138045",
"var3":"6437921611015627107",
"var4":"-6020554262958932056",
"var5":"-3528369270255742994"
}

Another way to solve this situation is to make all necessary function calls in one formula - each time function will be called again. This code:

FORMULA[
'{"var1":"' + TEXT(RANDOM()) + 
'","var2":"' + TEXT(RANDOM()) + 
'","var3":"' + TEXT(RANDOM()) +
'","var4":"' + TEXT(RANDOM()) +
'","var5":"' + TEXT(RANDOM()) +
'"}']

will return correct result (JSON text):

{"var1":"-7277966880876800456","var2":"-3968577798298648346","var3":"-883462064986564523","var4":"99229509701172936","var5":"8738940707593593542"}

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.