JavaScript

Currently Being Moderated

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

Apr 19, 2012 10:41 AM

Tags:

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

Replies
• 9,232 posts
Aug 11, 2002
Currently Being Moderated
Apr 19, 2012 12:11 PM   in reply to Phillips99

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

|
Mark as:
• Currently Being Moderated
Apr 19, 2012 12:54 PM   in reply to Phillips99

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.

|
Mark as:
• 9,232 posts
Aug 11, 2002
Currently Being Moderated
Apr 19, 2012 12:54 PM   in reply to Phillips99

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

|
Mark as:
• Currently Being Moderated
Apr 19, 2012 1:15 PM   in reply to Phillips99

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

|
Mark as:
• 9,232 posts
Aug 11, 2002
Currently Being Moderated
Apr 19, 2012 1:16 PM   in reply to Phillips99

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

|
Mark as:
• 9,232 posts
Aug 11, 2002
Currently Being Moderated
Apr 19, 2012 1:52 PM   in reply to Phillips99

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

|
Mark as:
• Currently Being Moderated
Apr 19, 2012 2:43 PM   in reply to Phillips99

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.

|
Mark as:
• Currently Being Moderated
Apr 20, 2012 8:55 AM   in reply to Phillips99

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.

|
Mark as:
• 9,232 posts
Aug 11, 2002
Currently Being Moderated
Apr 25, 2012 11:03 AM   in reply to Phillips99

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.

|
Mark as:
• Currently Being Moderated
Apr 25, 2012 12:07 PM   in reply to George Johnson

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!

|
Mark as:
Actions

More Like This

• Retrieving data ...

Bookmarked By (0)

• 10 points awarded for Correct Answers
• 10,000+ points
• 1,001-10,000 points
• 501-1,000 points
• 5-500 points