Copy link to clipboard
Copied
Here's a code I somehow manage to get. The only problem is It won't work unless all variables have a number in the field. How can I make a variable thinks it's zero when I don't fill in the field?
var v1 = 0; var v2 = getField("MRow1").value; var v3 = getField("TRow1").value; var v4 = getField("WRow1").value; var v5 = getField("TRow1_2").value; var v6 = getField("FRow1").value; var v7 = getField("SRow1_2").value; var v8 = getField("RegRow1").value; var v9 = getField("OverRow1").value; var v10 = getField("SRow1").value; event.value = (v1 + v10 + v2 + v3 + v4 + v5 + v6 + v7) - v9;
Change all of these lines:
var v2 = getField("MRow1").value;
To:
var v2 = +getField("MRow1").value;
Copy link to clipboard
Copied
Change all of these lines:
var v2 = getField("MRow1").value;
To:
var v2 = +getField("MRow1").value;
Copy link to clipboard
Copied
The "+" JavaScript operator can either add numbers or concatenate, join, strings. JavaScript determines how this operator will function by the type of of the values being processed. Strings are concatenated while numbers are added and other types of values or variables will result in unknown results. One can force a value to a number by using one of the number constrictors. This can be the "Number(value)" constrictor, multiplication by 1 or by prefixing the value with a "+" symbol.
Copy link to clipboard
Copied
Very helpful thank you! I have one more issue though. Now it is skewing my total at the bottom. All I have on the total field is a simple "Value is the sum of the following fields:" This field is totaling 15 fields using the script above.
Copy link to clipboard
Copied
Make sure the field calculation order is correct.
Copy link to clipboard
Copied
With the same calculation being performed in so many fields, consider creating a document level function that takes the field names as parameters and returns the calculated amount or sets the field value. It might also be possible to compute a variable or variables that could be used as the field name. The result will be having on calculation script to edit if any change needs to be made to the calculation. I would also look at using the built-in format for the numeric fields since this will limit user entry to only numbers and you might be able to limit the range of values that could be entered. You can also make fields read only so the user cannot modify the contents. There is no need for a user to try to edit a calculated field value.
In the future you might want to look at creating s single row of fields and then using the "create multiple copies" right mouse click options. This will make a number of other tasks easier with some planning.