Live Forms v5.1 is no longer supported. Click here for information about upgrading to our latest GA Release.

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Why do I need a local variable for certain cases ?

There are rules scenarios where you can't use a frevvo control directly in a rule expression and you are forced to define a local variable, perform the computation using the local variable and assign the value to the control at the conclusion of the computation.

For instance, assume a form that calculates a total from a list of subtotals. The total is a control T and subtotals are represented as a repeat of controls S. In other words, T holds the total of adding all values in S. You could write the rules as:

T.value = 0; 
for (var i = 0; i < S.value.length; i++) {   
    T.value = T.value + S[i].value; 
} 

The code above makes logical sense but it just won't work. In a nutshell this particularity of the frevvo implementation can be resumed this way:

The right hand side of an assignment expression resolves to the value of a frevvo control passed to a rule when it is invoked. The left hand side uses a reference to the control''.

Lets work with a concrete example to understand the concept. Assume that there are two subtotals in the form, 12 and 14 and the total is correctly set to 26. Now, you add 50 as a third subtotal and the rule fires as a consequence. The values of the controls involved in the rule are passed as parameters to the rule: the subtotals 12,14 and 50 and the current total (value of T), 26. The core of the matter is in the expression:

T.value = T.value + S[i].value; 

The expression will be evaluated three times (since there are three subtotals) but for every iteration, the T.value operand on the right hand side will always evaluate to 26 which is not the desired behavior and will lead to an incorrect result. Again, this happens because the right hand side of that expression is resolved based on the values initially passed to the rule as parameters.

Frevvo explicitly imposes this restriction for the sake of efficiency and there would be a significant impact in performance if the restriction was not in place. Lets rewrite the rule using a local variable:

var tot = 0; 
for (var i = 0; i < S.value.length; i++) {   
    tot = tot + S[i].value; 
} 
T.value = tot; 

This rule will produce the expected result.

Why does my rule not execute?

A rule will not execute when circular logic is detected. Currently circular dependencies are not permitted. This example tries to set the value of the same control used to trigger the rules execution. This is not permitted and this rule will not execute. This restriction may be lifted in a future release.

if (color.value == 'red') {   
    color.value = 'blue'; 
} 

No execution error will be logged in frevvo server log files (see [[#Rule_Debugging | rule debugging]] above). The rule will simply not be executed.

  • No labels