Copy link to clipboard
Copied
Hi,
I am converting a document to a form and there is a ratings section with 6 categories where a rating of 1 to 5 is given. A read only field Total-Score calculates the ratings given for each category.
However, one category Kitchen, is only required if it a kitchen is present. Thus, a radio button with Yes and No option is present and if Yes is selected, the Kitchen-Rating field goes from hidden to visible, allowing the user to enter a rating. Otherwise, if No is selected, the field remains hidden.
How do I go create a custom calculation script to sum up these fields yet factor in the change of state of the Kitchen-Rating field, i.e. if hidden it is not included and if visible it is included? Below is my attempt to create the script but I receive the error message "SyntaxError: missing : after property id 3: at line 4".
if (this.getField("Kitchen-Rating").display = display.visible);
event.value = {
this.getField("Bathroom-Rating").value + this.getField("Beds-Rating").value + this.getField("Kitchen-Rating").value + this.getField("Walls-Rating").value + this.getField("Furniture-Rating").value + this.getField("Safety-Rating").value
} else event.value = {
this.getField("Bathroom-Rating").value + this.getField("Beds-Rating").value + this.getField("Walls-Rating").value + this.getField("Furniture-Rating").value + this.getField("Safety-Rating").value
};
Any suggestions on where I am going wrong?
Much thanks in advance.
Tim
1 Correct answer
Bernd's code will work, but I would prefer to do it like this, to make the relation between the visibility of the field and adding its value to the total more clear:
...var total = Number(this.getField("Bathroom-Rating").valueAsString) + Number(this.getField("Beds-Rating").valueAsString) +
Number(this.getField("Walls-Rating").valueAsString) + Number(this.getField("Furniture-Rating").valueAsString) +
Number(this.getField("Safety-Rating").valueAsString);
if (this.getField("Kitchen-Rating").di
Copy link to clipboard
Copied
Use this:
if (this.getField("Kitchen-Rating").display == display.visible) {
event.value = this.getField("Bathroom-Rating").value + this.getField("Beds-Rating").value + this.getField("Kitchen-Rating").value + this.getField("Walls-Rating").value + this.getField("Furniture-Rating").value + this.getField("Safety-Rating").value;
} else {
event.value = this.getField("Bathroom-Rating").value + this.getField("Beds-Rating").value + this.getField("Walls-Rating").value + this.getField("Furniture-Rating").value + this.getField("Safety-Rating").value;
};
Copy link to clipboard
Copied
Bernd's code will work, but I would prefer to do it like this, to make the relation between the visibility of the field and adding its value to the total more clear:
var total = Number(this.getField("Bathroom-Rating").valueAsString) + Number(this.getField("Beds-Rating").valueAsString) +
Number(this.getField("Walls-Rating").valueAsString) + Number(this.getField("Furniture-Rating").valueAsString) +
Number(this.getField("Safety-Rating").valueAsString);
if (this.getField("Kitchen-Rating").display == display.visible)
total+=Number(this.getField("Kitchen-Rating").valueAsString);
event.value = total;
I also added an explicit conversion of the values of the fields to numbers, which is a good practice in general.
Copy link to clipboard
Copied
Thank you try67​ for the code. The fields being calculated are formatted to whole numbers so would the conversion of the values to numbers still be required?
Appreciated the good practice tip
Copy link to clipboard
Copied
Yes, because if one of them is empty the "+" operator might concatenate the values as strings, instead of adding them up as numbers.

