11 Replies Latest reply on Nov 2, 2017 8:35 AM by try67

# Custom Calculation Script for PDF Form

Hello,

I have no javascript experience at all; however, I learned by searching on the internet that I need to create a Custom Calculation Script on my .pdf form to get it to do what I want. I researched and attempted it myself and was unsuccessful.

I have four fields created that are named:

• NumofCriteriaMet
• TotalNumofCriteria
• PercentofCriteriaMet
• CPRatingScore

Currently, I have a simplified field notation (division) in the PercentofCriteriaMet as follows: NumofCriteriaMet/TotalNumofCriteria

and it is working.

What I am trying to do is take the number auto-populated from PercentofCriteriaMet and assign a value based on the range below and have the value auto-populated in CPRatingScore

0.0 to 0.59 = 0

0.60 - 0.89 = 1

0.90 - 1.00 = 2

Also, I don't know if it makes a difference, but I currently have the PercentofCriteriaMet and CPRatingScore field set to "read only"

If anyone can assist in helping me create a working Custom calculation script, it would be much appreciated.

Shelly

• ###### 1. Re: Custom Calculation Script for PDF Form

As the custom calculation script of CPRatingScore enter this code:

```var percCriteriaMet = Number(this.getField("PercentofCriteriaMet").value);
if (percCriteriaMet>=0 && percCriteriaMet<=0.59) event.value = 0;
else if (percCriteriaMet<=0.89) event.value = 1;
else if (percCriteriaMet<=1) event.value = 2;
else event.value = "";
```

It doesn't matter that the fields are read-only. However, what does matter is the calculation order. You need to make set it so that CPRatingScore is calculated after PercentofCriteriaMet.

Also, you might have problems with the calculation you created for PercentofCriteriaMet. When the divisor field (TotalNumofCriteria) is empty or equals zero that will cause an error, as division by zero is not allowed. To avoid that you would need to use a custom calculation script that only applies the formula if the divisor is not zero. That code could look something like this:

```var v1 = Number(this.getField("NumofCriteriaMet").value);
var v2 = Number(this.getField("TotalNumofCriteria").value);
if (v2==0) event.value = "";
else event.value = v1/v2;
```
• ###### 2. Re: Custom Calculation Script for PDF Form

Good morning try67.

Thank you for your help! I plugged the code into the CPRatingScore field's custom calculation script along with the code you provided for the PercentofCriteriaMet field and it is somewhat working.

The code for PercentofCriteriaMet is working perfectly; however the value in the CPRatingScore field does not update properly.  For example, I inputted a value of "8" in NumofCriteriaMet and a value of "12" in TotalNumofCriteria which auto-populated a percent of 66.67% in PercentofCriteriaMet, but the CPRatingScore value did not change to "1" like it should.  However, when I changed the NumofCriteriaMet value to another number, "10", the CPRatingScore changed to the correct value. When I changed the NumofCriteriaMet value a third time, the CPRatingScore failed to update again.

Attempt #Field: NumofCriteriaMetField: TotalNumofCriteriaField: PercentofCriteriaMet (auto-filled)

Field:

CPRatingScore (auto-filled)

181266.67%0 (incorrect)
2101283.33%1 (correct)
31128.33%1 (incorrect)

Is this the calculation order problem you mentioned? If so, how do I correct it?

Shelly

• ###### 3. Re: Custom Calculation Script for PDF Form

What version of Acrobat do you use?

• ###### 5. Re: Custom Calculation Script for PDF Form

11.0.14

• ###### 6. Re: Custom Calculation Script for PDF Form

Then you'll find it in Form Edit mode (Tools - Forms - Edit), under Tasks - More Tasks - Set Field Calculation Order...

• ###### 7. Re: Custom Calculation Script for PDF Form

Try67 -

You're awesome! It works perfectly now.  Thank you so much for your help.

• ###### 8. Re: Custom Calculation Script for PDF Form

OK, I am having a similar problem... I know I needed Java code to write the equation into PDF forms, the problem I'm having is getting the code to match the formula. I have tried multiple variations. Last night I realized I was computing a unneeded number into it by leaving the 101 (so as to dismiss any unseen cells in excel), took it out and still cant find the right one. After spending MOST OF (0700-1400) the day on it, I decided to start keeping track of the codes I was trying to keep from retrying them; I was getting tired of the noise Adobe makes explaining syntax errors (TRY AGAIN STUPID). Here is a (non-comprehensive) list of the codes I used yesterday.

(function () {

var v1=this.getField("KDBI").value;

var v2=this.getField("CATLVLWINDS").value;

var v3= this.getField("CATLVLRELHUMD").value;

var v4=this.getField("CATLVLKDBI").value;

Event.value=(v1<600,ROUNDUP(SUBTOTAL(v2+v3+v4),0),5);

})();

var v1=this.getField("KDBI").value;

var v2=this.getField("CATLVLWINDS").value;

var v3= this.getField("CATLVLRELHUMD").value;

var v4=this.getField("CATLVLKDBI").value;

Event.value=(v1<600,ROUNDUP(SUBTOTAL(101,v2,v3,v4),0||5);

var v1=this.getField("KDBI").value;

var v2=this.getField("CATLVLWINDS").value;

var v3= this.getField("CATLVLRELHUMD").value;

var v4=this.getField("CATLVLKDBI").value;

Event.value=(v1<600,ROUNDUP(SUBTOTAL(101,v2,v3,v4),0),5);

(function () {

var v1=this.getField("KDBI").value;

var v2=this.getField("CATLVLWINDS").value;

var v3= this.getField("CATLVLRELHUMD").value;

var v4=this.getField("CATLVLKDBI").value;

Event.value=(v1<600,ROUNDUP(SUBTOTAL(101,v2,v3,v4),0):5);

})();

var v1=this.getField("KDBI").value;

var v2=this.getField("CATLVLWINDS").value;

var v3= this.getField("CATLVLRELHUMD").value;

var v4=this.getField("CATLVLKDBI").value;

If(KDBI>600,ROUNDUP(SUBTOTAL(101,CATLVLWINDS,CATLVLRELHUMD,CATLVLKDBI)),0:5);

(RELHUMD>=50?2:(RELHUMD>=40?3:(RELHUMD>=30?4:(RELHUMD<=29?5:0))))

This is the Excel formula I need to convert //

=IF(B14<600,ROUNDUP(SUBTOTAL(101,C12:C14),0),5)       //        Where KDBI = B14; CATLVLWINDS = C12; CATLVLRELHUMD = C13; CATLVLKDBI = C14

• ###### 9. Re: Custom Calculation Script for PDF Form

Instead of providing a list of things that didn't work and Excel formulas, could you explain what exactly you're trying to achieve, in simple terms?

• ###### 10. Re: Custom Calculation Script for PDF Form

Hey everyone,  I don't write scripts in a PDF docs, however, I need a quick solution for a document I have.

As it is layed out of the PDF document

box 1a         box 1b        box 2a        box 2b         box 3a        box3b           4c

(\$amount * # of days) + (\$amount * # of days) + (\$amount * # of days) = total cost

Thanks

• ###### 11. Re: Custom Calculation Script for PDF Form

Use this code as the custom calculation script of "4c":

var v1 = Number(this.getField("box 1a").valueAsString);

var v2 = Number(this.getField("box 1b").valueAsString);

var v3 = Number(this.getField("box 2a").valueAsString);

var v4 = Number(this.getField("box 2b").valueAsString);

var v5 = Number(this.getField("box 3a").valueAsString);

var v6 = Number(this.getField("box 3b").valueAsString);

event.value = (v1*v2)+(v3*v4)+(v5*v6);