Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Custom calculation script involving checkboxes resulting in different things

Explorer ,
Mar 17, 2020 Mar 17, 2020

Complete Javascript novice here. I'm creating a form which is fairly complex and I can't get a ticked checkbox to even fill a text feild let alone do what I actually need it to do. Hopefully someone can help with the code I need.

 

The form involves clients listing products and $ value investments they would like for each product. Those investments fill in a 'Subtotal' field easily with a sum. Depending on the amount of products they are getting, a discount can be applied. 2 products = 10% off, 3 products = 15% off, 4+ products = 20% off. Below the Subtotal field I want a Subtotal with discount applied (SubtotalDiscount) field.  I've currently got checkboxes for the three discount options. When one of the checkboxes are ticked, I want a calculation script to take the value in the 'subtotal' field, calculate the corresponding %, then add that value to the 'subtotal' value and place it in the 'SubtotalDiscount' field. 

 

Does anyone have any clue on the code I would need for this?

TOPICS
General troubleshooting , PDF forms
2.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Explorer ,
Mar 17, 2020 Mar 17, 2020

I got it working with some different code. Hooray! For anyone's reference, this is what I added to the 'SubtotalDiscount' field custom calculation script...

 

var percentage = 0;

if (this.getField("2Products").value == "On") { percentage = 0.10 }

if (this.getField("3Products").value == "On") { percentage = 0.15 }

if (this.getField("4Products").value == "On") { percentage = 0.20 }

var subtotal = this.getField('Subtotal').value;

var discount = subtotal * percentage;

this.event.value = subtotal - discount;

 

Tomorrow I might change them from checkboxes to radial buttons so that they can't tick two at a time, but I think that won't be too hard. Have also rearranged them so that if two checkboxes are ticked that it prioritises the largest discount.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2020 Mar 17, 2020

Here an article that will get you going in the right direction. It includes a sample file that uses a checkbox. 

https://www.acrobatusers.com/tutorials/conditional-execution/

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 17, 2020 Mar 17, 2020

Thank you. I have already read through all of that and tried to use the sample file code, but it's not doing the same things as I need it to do and without knowing how to write and read javascript I'm a bit lost!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 17, 2020 Mar 17, 2020

Use a calculation script on the SubtotalDiscount field

 

var cDiscountSelection = this.getField("Checkbox").valueAsString;

if(cDiscountSelection == "choice1")

   event.value = this.getField("Subtotal").value * .9;

else  if(cDiscountSelection == "choice2")

   event.value = this.getField("Subtotal").value * .8;

... etc ..

 

 

I don't know the name of the checkbox group or the export values, so you'll need to replace those in the code. The "Subtotal", as well as all the other field names have to be verbatim. Capital letters and all. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 17, 2020 Mar 17, 2020

Thanks Thom!

I've tried that out. I have my 3 checkboxes grouped and have their respective export values now. I've got my code on the SubtotalDiscount text field. Saved. But checking any of the boxes isn't putting any value into the field. Can you spot what I'm doing wrong?

 

Screen Shot 2020-03-18 at 4.16.05 pm.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2020 Mar 18, 2020
LATEST

There's nothing wrong with the code. But perhaps the values and/or field names aren't lining up quite right.

We'll need to do some debug.

Look in the Console window to see if any errors are reported. 

There is a tutorial here, on using the console and doing some debug.

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 17, 2020 Mar 17, 2020

I got it working with some different code. Hooray! For anyone's reference, this is what I added to the 'SubtotalDiscount' field custom calculation script...

 

var percentage = 0;

if (this.getField("2Products").value == "On") { percentage = 0.10 }

if (this.getField("3Products").value == "On") { percentage = 0.15 }

if (this.getField("4Products").value == "On") { percentage = 0.20 }

var subtotal = this.getField('Subtotal').value;

var discount = subtotal * percentage;

this.event.value = subtotal - discount;

 

Tomorrow I might change them from checkboxes to radial buttons so that they can't tick two at a time, but I think that won't be too hard. Have also rearranged them so that if two checkboxes are ticked that it prioritises the largest discount.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 17, 2020 Mar 17, 2020

That last line of code should be:

 

event.value = subtotal - discount;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2020 Mar 18, 2020

Name all of your radio buttons the same and give them different export values, and they'll act just like radio buttons. But whether or not you use radio buttons or checkboxes, you'll need to use the code I provided in the first post. 

 

Also, it is best to write the "if" block as an "else if" so that there is only one path to each answer. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines