Problem with calculations fields, can you help me?

Jan 16, 2018

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?

fields_5.PNG

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

99

Likes

Translate

Translate

Report

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

Problem with calculations fields, can you help me?

Jan 16, 2018

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?

fields_5.PNG

Adobe Community Professional
Correct answer by Thom Parker | Adobe Community Professional

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?

TOPICS
Acrobat SDK and JavaScript, Windows

Views

100

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 16, 2018 0
Adobe Community Professional ,
Jan 16, 2018

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 16, 2018 0
Jan 17, 2018

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:

fields_5.PNG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2018 0
Adobe Community Professional ,
Jan 17, 2018

Copy link to clipboard

Copied

Read the reply from Thom Parker.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2018 0
Most Valuable Participant ,
Jan 17, 2018

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2018 1
Jan 17, 2018

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2018 0
Adobe Community Professional ,
Jan 17, 2018

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2018 1
Adobe Community Professional ,
Jan 17, 2018

Copy link to clipboard

Copied

Use a validation script for the dropdown list.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 17, 2018 2