18 Replies Latest reply on Apr 25, 2012 12:07 PM by DanFlamma1

# Formula to divide three field entries (D=A/B/C)

I have a formula to obtain a value and it involves dividing three field entries. I don't know enough about javascript to correctly enter the code to make this happen. I have 20 rows in a form and I need to calculate column D. I saw a thread on how to divide two fields but I need to go further with that. Can anyone help me? I've added what I was working from (left) and what I tried to develop (right).

InternetPersonal

// establish variables for the field names for the dividend and divisor.

var sDividend = 'My Dividend Field Name';

var sDivisor = 'My Divisor Field Name';

// get the value of the divisor field

var nDivisor = this.getField(sDivisor).value;

// get the value of the dividend field

var nDividend = this.getField('sDividend').vlalue;

// do not change the code below this line

// clear the result field value

event.value = '';

// see if we have a non-zero divisor

if (nDivisor != 0) {

// we can now divide

event.value = nDividend / nDivisor;

} // end division calculation

The above assumes your are dividing the values of 2 fields.

// Assigning the variables

var PW1 = "PlotWt1";

var WBC1 = "WetbuConversion1";

var AF1 = "Acre Factor1";

// Getting the values

var nPW1 = this.getField(PW1).value;

var nWBC1 = this.getField(WBC1).value;

var nAF1 = this.getField(AF1).value;

// clear the result field value

event.value="";

// see if we have a non-zero divisor

if (nWBC1 !=0) {

//Actual division operation

event.value = nPW1 / nWBC1 / nAF1;

}// end of division calculation

• ###### 1. Re: Formula to divide three field entries (D=A/B/C)

In what way is the script not working? Will the AF1 field ever be blank or otherwise evaluate to zero?

• ###### 2. Re: Formula to divide three field entries (D=A/B/C)

The script is not returning a value to the field I'm trying to calculate. It just remains blank. It is an all or nothing type of form. If any of the cells have data they will all have data. The reason I only check to see if nWBC1 is zero is because I thought I could make it work by removing AF1 from the equation. That did not yield any results either.

• ###### 3. Re: Formula to divide three field entries (D=A/B/C)

You can add the try statement to catch and report an error.

You can add console print statements to display the process of the script, variable values, and results of calculations before setting a field value.

JavaScript runs terminate upon an error unless you trap the error.

• ###### 4. Re: Formula to divide three field entries (D=A/B/C)

Do you see any errors in the JavaScript console (Ctrl+J)?

• ###### 5. Re: Formula to divide three field entries (D=A/B/C)

Was unaware of such a console. I did see an error:

TypeError: this.getField(AF1) is null
9:Field:Calculate

• ###### 6. Re: Formula to divide three field entries (D=A/B/C)

I'm too unfamiliar with the appropriate syntax to perform any of those tasks. My Javascript knowledge is almost null.

• ###### 7. Re: Formula to divide three field entries (D=A/B/C)

And my next question once we've cracked this little chestnut is: Is there an easy way to apply this calculation to all 20 rows of the form or do I need to copy/paste and manipulate each row individually. In Excel I'd simply copy it to the bottom but that just don't seem possible in this program.

• ###### 8. Re: Formula to divide three field entries (D=A/B/C)

Strings (like field names) need to be placed between double-quotes.

• ###### 9. Re: Formula to divide three field entries (D=A/B/C)

What that means is there is no field with the name "Acre Factor1". So to correct it, use the exact name of the field.

• ###### 10. Re: Formula to divide three field entries (D=A/B/C)

I removed the space and the code works! Two followup questions....

What do I need to add to check if nAF1 is zero?

Can I apply this across all 20 rows easily or do I have to copy/paste/manipulate as I mentioned above?

• ###### 11. Re: Formula to divide three field entries (D=A/B/C)

Change the one line of code to:

if (nWBC1 !=0 && nAFN != 0) {

If you want to use the same calculation for a number of rows, you can create a function that all of the calculated fields can use, but it would depend on the field names you're using. If you're just changing the number at the end, the function could be:

```function calc1(num) {

// Assigning the variables
var PW = "PlotWt" + num;
var WBC = "WetbuConversion" + num;
var AF = "AcreFactor1" + num;

// Getting the values
var nPW = this.getField(PW).value;
var nWBC = this.getField(WBC).value;
var nAF = this.getField(AF).value;

// clear the result field value
event.value="";

// see if we have a non-zero divisor
if (nWBC !=0 && nAF != 0) {
//Actual division operation
event.value = nPW / nWBC / nAF;
}

}
```

You can then call it in the field's custom Calculate script like:

calc1(1);  // Row 1

For row #5, it would be:

calc1(5);  // Row 5

This could be simplified a bit if you changed your field naming scheme to make it easier to extract the row number. That way you wouldn't have to pass it as I've shown.

To create a document-level script to place the function, select: Advanced > Document Processing > Document JavaScripts > Add

• ###### 12. Re: Formula to divide three field entries (D=A/B/C)

You could pass the field names a parameter in the function and then use the parameter value in the function to access the form field.

• ###### 13. Re: Formula to divide three field entries (D=A/B/C)

GKaiseril wrote:

You could pass the field names a parameter in the function and then use the parameter value in the function to access the form field.

I still don't know how to program any Java and as such don't know how to pass anything a parameter although it sounds like that could be helpful.

• ###### 14. Re: Formula to divide three field entries (D=A/B/C)

I have created the document-level script without errors from the code you provided (cut and paste). But when I put calc1(2), calc1(3), etc in the respective field's custom calculation box it is throwing back this error:

TypeError: this.getField(PW) is null
9:Field:Calculate
TypeError: this.getField(PW) is null
9:Field:Calculate

You can see that it shows up twice for each individual field. I've entered values in those fields in the Preview mode and gone back into edit and it gives the same error. The strange part is that calc1(1) works properly.

• ###### 15. Re: Formula to divide three field entries (D=A/B/C)

Acrobat uses JavaScript and that language is very different from Java.

We need to know the exact name of the fields or how you structured you field naming.

• ###### 16. Re: Formula to divide three field entries (D=A/B/C)

Unfortunately I don't know either language. Your response did spark a revelation though, I changed some field names and now it all works correctly with the code provided above. Thanks for that!

I do have an additional issue though. I've been working on this in Abobe Professional 9.4 and I've developed my PDF form that I want my users to fill in but when I open the form in reader the D column no longer calculates. I'm not sure if I had to do a final "publishing" step to make the form operational but if you could point me in the right direction that would be great.

• ###### 17. Re: Formula to divide three field entries (D=A/B/C)

Be sure to check the JavaScript console (Ctrl+J) for any errors after changing some field values, then check to make sure the field calculation order is correct.

• ###### 18. Re: Formula to divide three field entries (D=A/B/C)

George- I am not sure if you got my emails about the Extended Care Calculator but Andy and I would like to see what we need to do to finish that up.  Thanks!