Skip to main content
tim-b
Participating Frequently
November 24, 2018
Answered

Custom calculation script to omit field if it is hidden

  • November 24, 2018
  • 2 replies
  • 978 views

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

This topic has been closed for replies.
Correct answer try67

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.

2 replies

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 24, 2018

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.

tim-b
tim-bAuthor
Participating Frequently
November 24, 2018

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

try67
Community Expert
Community Expert
November 24, 2018

Yes, because if one of them is empty the "+" operator might concatenate the values as strings, instead of adding them up as numbers.

Bernd Alheit
Community Expert
Community Expert
November 24, 2018

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;

};