Skip to main content
Participant
March 27, 2023
Answered

Acrobat PDF calculation script

  • March 27, 2023
  • 1 reply
  • 3432 views

I can handle simple calculation scripts, but was hoping someone could help me with one that is a bit more complicated. TIA!

 

I have a form that is a combination of radio buttons and text fields. The user can choose ONE option from Membership Level (which returns the specific dollar amount) and has the option of writing in an additional dollar amount if they choose. Currently a simple sum calculation returns the total amount in the corresponding field based on the user's Membership Level choice and any amount they write in. However, they would now like an optional radio button allowing the user to select a 10% discount. What I need to happen is for the Total field to calculate the 10% discount from the combined total of the other options IF this radio button is selected. Is this doable?

 

I've included a photo of the form. Please let me know if any of this is unclear. Thanks!

This topic has been closed for replies.
Correct answer Nesa Nurani

Any luck?


There is not a "Membership" field in your file instead you have radio buttons named "Level" so try using this script:

var v1 = this.getField("Level"); 
var v2 = Number(this.getField("Amount").valueAsString); 
var total = 0; 
if(v1.valueAsString != "Off")total= Number(v1.valueAsString)+v2;
if (this.getField("Discount").valueAsString!="Off") 
total*=0.9; 
event.value = total;

 I would also set "Amount" field to be read only until user checks "Additional Donation" checkbox.

1 reply

try67
Community Expert
Community Expert
March 27, 2023

You can use something like this as the total field's custom Calculation script (adjust the field names as needed):

 

var v1 = Number(this.getField("Membership Level").valueAsString);
var v2 = Number(this.getField("Additional Amount").valueAsString);
var total = v1+v2;
if (this.getField("Discount").valueAsString!="Off") total*=0.9;
event.value = total;
Participant
March 28, 2023

Thanks so much for the quick response. I tried this and applied it to the "Total" field, but I'm not getting any action. I renamed the fields as follows. FYI: To make sure it wasn't a naming issue I did try the simple sum action and it worked. Any idea?

Nesa Nurani
Community Expert
Community Expert
March 28, 2023

Did you put script in 'Custom calculation script'?

Can you share actual file with us?