Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Custom calculation script to omit field if it is hidden

Community Beginner ,
Nov 23, 2018 Nov 23, 2018

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

TOPICS
Acrobat SDK and JavaScript , Windows
864
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 24, 2018 Nov 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").di

...
Translate
Community Expert ,
Nov 23, 2018 Nov 23, 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;

}; 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 24, 2018 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 24, 2018 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 24, 2018 Nov 24, 2018
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines