Copy link to clipboard
Copied
I created the following code for the field with the number 30:
var x = this.getField("valor_col").value;
var y = this.getField("esc_col").value; // another field (dropdown) that receives only color names (strings)
if (y == "red" ) {
event.value = x + 4
}
else if (y == "blue" ) {
event.value = x + 2
}
else if (y == "green" ) {
event.value = x - 2
}
else {
event.value = x + 1
}
The code is working correctly, the problem happens when I type any value in the other fields that do not yet have javascript code. For example, when I choose the blue color, my field should add 2 to the total, this actually happens, but when I type any value in any other field it adds 2 more to the value of the field that has the code I created (for example), and this should not to happen. Can you help me?
Calculation scripts are called every time any field changes. You need to setup the calculation to work the same way every time. If the calculated value is incremented, then is x from the value of the field with the calculation on it? Otherwise, how could it increase?
Copy link to clipboard
Copied
Calculation scripts are called every time any field changes. You need to setup the calculation to work the same way every time. If the calculated value is incremented, then is x from the value of the field with the calculation on it? Otherwise, how could it increase?
Copy link to clipboard
Copied
Thank you for your quick answer,
But that's the problem, I do not want this increment to happen, it should not happen. In my understanding the other fields should not influence anything in the field that I set.
See if with this image my question is clearer:
Copy link to clipboard
Copied
Read the reply from Thom Parker.
Copy link to clipboard
Copied
Instead of using a script which increments the current field's value (which is not going to work, due to the reasons described above), you need to write a script that calculates the full value each time it is launched.
The only other option is to add a condition to the code that only changes the field's value if the event was triggered by specific fields.
Copy link to clipboard
Copied
try67​
Thanks for the help, the problem is that I do not know how to do this, could you help me?
Copy link to clipboard
Copied
The problem is your calculation goes in circles. The field is setup to increment itself. To control this the script has to have extremely rigorous conditions for performing the calculation, or it needs to be driven from specific user actions. Please define the exact behavior of your form. Writing down a concise explanation of what you want can make things much clearer.
Why is it necessary for the calculation increment itself?
Copy link to clipboard
Copied
Use a validation script for the dropdown list.