These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Needing assist with Field Calculations

Zeroh Blue
Registered: Aug 4 2010
Posts: 8
Answered

JS newbie here looking for assistance.
I have a form with multiple fields, in this instance Fields A, B and C
A second variable comes to play with fields A1, B1 and C1.
The second variables are determined from a drop down list:
var A1;
switch(this.getField("X").value) {
case 'SELECT VALUE' : X = ' '; break;
case 'ALPHA' : X = +1; break;
case 'BRAVO' : X = +2; break;
case 'CHARLIE' : X = +3; break;
case 'DELTA' : X = +4; break;
case 'ECHO' : X = +5;
}
event.value = X
 
var B1;
switch(this.getField("X").value) {
case 'SELECT VALUE' : X = ' '; break;
case 'ALPHA' : X = +0; break;
case 'BRAVO' : X = +0; break;
case 'CHARLIE' : X = +0; break;
case 'DELTA' : X = +0; break;
case 'ECHO' : X = +0;
}
event.value = X
 
var C1;
switch(this.getField("X").value) {
case 'SELECT VALUE' : X = ' '; break;
case 'ALPHA' : X = +5; break;
case 'BRAVO' : X = +4; break;
case 'CHARLIE' : X = +3; break;
case 'DELTA' : X = +2; break;
case 'ECHO' : X = +1;
}
event.value = X
  
I want to calculate the total of field A + field A1, etc.
var A = this.getField("A").value;
var A1 = this.getField("A1").value;
event.value = A + A1
 
var B = this.getField("B").value;
var B1 = this.getField("B1").value;
event.value = B + B1
 
var C = this.getField("C").value;
var C1 = this.getField("C1").value;
event.value = C + C1
 
At first the fields calculate properly.
Values inserted in fields A, B and C
A = 1
B = 2
C = 3
 
The drop down field "ALPHA" is selected
A1 = 1
B1 = 0
C1 = 5
 
fields A, B and C now calculate to:
A = 2
B = 2
C = 8
 
When I updated the value for instance field A from 1 to 3, fields B and C update and calculate again.
A = 4
B = 2
C = 13
 
How do i prevent the other fields from "auto" calculating and keep the values in the other fields static?

My Product Information:
Acrobat Pro 9.4, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
It's really not clear to me how you have things set up. Are fields A, B, and C active fields that a user can enter a number into, or are they set up as calculated fields, or are you attempting to have them be both?

Where did you place the following code, exactly? Which field and which event?

var A = this.getField("A").value;
var A1 = this.getField("A1").value;
event.value = A + A1

If you could post a sample somewhere, it would be very helpful.

Also, code like the following:

case 'SELECT VALUE' : X = ' '; break;

will cause trouble since it's possible for the user to select this option, which will result in the value of the field getting set to a single space character. In a subsequent calculation that looks like you're wanting numeric addition, as opposed to string concatenation, this will cause trouble.

Finally, always make sure that the field calculation order makes sense for your form.
Zeroh Blue
Registered: Aug 4 2010
Posts: 8
The intent was to have fields A, B and C be both fields you can input values and also be calculated.

The code was inbedded into each corresponding field.

var a => field A... etc.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
OK, that's a bit unusual, but it is possible. Just to clarify, is the following code currently the complete custom calculate script for field A:

var A = this.getField("A").value;
var A1 = this.getField("A1").value;
event.value = A + A1


Zeroh Blue
Registered: Aug 4 2010
Posts: 8
That was the intial idea I had for the code.

only because the simplified field notation of A+A1 or the "Value is the sum" option didnt work.

I have a sample of what had intended but not sure how to submit it to you for review.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
OK, try the following as the custom calculation script for the A field:

// Custom calculation script
(function () {

// Calculate this field only if it was this field that
// triggered the calculation by the user changing the value
if (event.source && event.source === event.target) {
event.value = +event.value + +getField("A1").value;
}

})();


But you should change the line to:

case 'SELECT VALUE' : X = 0; break;

unless you want it to have a space.
Zeroh Blue
Registered: Aug 4 2010
Posts: 8
BRILLIANT!

I have not one iota how to untangle that but thanks much for the assist.

now Ive got an itch to google the hell out of the code bits to figure out what you did.

Thanks again!
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
The null character is a string not a number.

The "+" operator is either the addition operator for Numbers or the concatenation operator for strings or a string and number.

You are converting a number to a string and the result of the nest operation to a string.

You need to look at the documentation for MDC JavaScript and the Acrobat JS API Reference. Also buy a good book on Acrobat and Acrobat forms like Ted Padova's Acrobat Bible.

George Kaiser