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

Sum numeric fields based on selected checkboxes

Community Beginner ,
Feb 18, 2017 Feb 18, 2017

Copy link to clipboard

Copied

I have an Adobe Acrobat form with seven (7) text boxes that contain numeric fields formatted as currency. These are the fees for each subject in a course.

Beside each fee there are two checkboxes that operate exclusively (like radio buttons). The Export Values for each of the two checkboxes are Enrol and RPL.

Here's a screenshot of part of the form...

The textbox field names are as follows...

E101-fee

E102-fee

E104-fee

E105-fee

E107-fee

F102-fee

F104-fee

At the bottom of the form is another currency text field named CourseFee (highlighted blue).

I need the course fee to sum the fees in the other seven text fields, but I only want to include those with the associated "Enrol" checkbox selected. If "RPL" is selected there will be no charge for that subject.

The "Enrol" and "RPL" checkboxes are automatically selected in specific configurations when exclusive checkboxes A, B, or C are selected (these are on another part of the form and are working perfectly). Clicking "A" selects all "Enrol" boxes, "B" selects "RPL" for 5 and "Enrol for 2, "C" selects "RPL" for 6 and "Enrol" for 1.

After this pre-selection occurs and the CourseTotal is displayed, any of the "Enrol" or "RPL" checkboxes can be subsequently changed and the CourseTotal is to update immediately.

Any help would be greatly appreciated.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

5.5K

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Feb 19, 2017 Feb 19, 2017

You can use this code as the custom calculation script of your CourseFee field:

var fields = ["E101", "E102", "E104", "E105", "E107", "F102", "F104"];

var total = 0;

for (var i in fields) {

    if (this.getField(fields).value=="Enrol") {

        total+=Number(this.getField(fields+"-fee").value);

    }

}

event.value = total;

Votes

Translate

Translate
Community Expert ,
Feb 19, 2017 Feb 19, 2017

Copy link to clipboard

Copied

What are the names of the check-box fields?

Votes

Translate

Translate

Report

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 Beginner ,
Feb 19, 2017 Feb 19, 2017

Copy link to clipboard

Copied

E101

E102

E104

E105

E107

F102

F104

Votes

Translate

Translate

Report

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 ,
Feb 19, 2017 Feb 19, 2017

Copy link to clipboard

Copied

You can use this code as the custom calculation script of your CourseFee field:

var fields = ["E101", "E102", "E104", "E105", "E107", "F102", "F104"];

var total = 0;

for (var i in fields) {

    if (this.getField(fields).value=="Enrol") {

        total+=Number(this.getField(fields+"-fee").value);

    }

}

event.value = total;

Votes

Translate

Translate

Report

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 Beginner ,
Feb 19, 2017 Feb 19, 2017

Copy link to clipboard

Copied

Thanks. Really simple yet elegant code. 🙂

Votes

Translate

Translate

Report

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
New Here ,
Feb 25, 2020 Feb 25, 2020

Copy link to clipboard

Copied

Your code is the simpliest and most elegant I have seen on these forums.  I have a similiar problem but cannot get it to work. My checkboxes are Option1, Option2, Option3, etc to Option8.  The fields with the values that need to be added are Option1-Cost, Option2-Cost, Option3-Cost, etc.  The export value of the checkboxes is "Yes".

 

This is the code I created from your code but cannot get it to work.  Can you help by chance?

 

var fields = ["Option1", "Option2", "Option3", "Option4", "Option5", "Option6", "Option7", "Option8"];

var total = 0;

for (var i in fields) {
    if (this.getField(fields).value=="Yes") {
        total+=Number(this.getField(fields+"-Cost").value);
    }
}
event.value = total;

 

Votes

Translate

Translate

Report

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 ,
Feb 26, 2020 Feb 26, 2020

Copy link to clipboard

Copied

Thanks!
In your case it's even easier since you don't need to use an array for the field names because they are consistent.

Use this:

 

var total = 0;

for (var i=1; i<=8; i++) {
    if (this.getField("Option"+i).valueAsString=="Yes") {
        total+=Number(this.getField("Option"+i+"-Cost").valueAsString);
    }
}
event.value = total;

Votes

Translate

Translate

Report

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
New Here ,
Feb 26, 2020 Feb 26, 2020

Copy link to clipboard

Copied

This is awesome.  Thank you so very much.  Worked like a charm!

Votes

Translate

Translate

Report

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
New Here ,
Apr 16, 2020 Apr 16, 2020

Copy link to clipboard

Copied

HI! I just tried this for myself and the code didn't work. Checkboxes are named "Course1", "Course2", "Course3", etc... Export value of checkboxes is "Yes". The field with values that need to be summed are "Course1-Credit", "Course2-Credit", etc...Some of the fields that need to be summed are static #s while a few are drop-down variables ranging from 0-4. Any help would be appreciated!!! 

Votes

Translate

Translate

Report

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 ,
Apr 16, 2020 Apr 16, 2020

Copy link to clipboard

Copied

What does "it didn't work" mean, exactly? Did it produce no results? The wrong results? Was there an error message in the JS Console? Please be more specific.

Votes

Translate

Translate

Report

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
New Here ,
May 28, 2020 May 28, 2020

Copy link to clipboard

Copied

Same here. I get no results whatsoever.

 

I used robs... field names just to ensure I have not made a mistake and still nothing. Am I doing something wrong?? Please help been at this for most of the day.

 

Screenshot 2020-05-28 at 23.28.05.pngScreenshot 2020-05-28 at 23.28.18.png

Votes

Translate

Translate

Report

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
New Here ,
Jul 31, 2020 Jul 31, 2020

Copy link to clipboard

Copied

Screen Shot 2020-07-31 at 1.45.54 PM.pngI've been trying to figure this out by reading all the other posts but I'm not getting anywhere. I want to add up all of my "Check Box" fields. I don't have (or know where it is) the "custom calculation script" box that I've seen on so many other posts. Am I in the wrong place all together?

Votes

Translate

Translate

Report

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 ,
Jul 31, 2020 Jul 31, 2020

Copy link to clipboard

Copied

The Calculate tab can be found under a text field, not under a check-box field.

Votes

Translate

Translate

Report

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
New Here ,
Aug 02, 2020 Aug 02, 2020

Copy link to clipboard

Copied

Screen Shot 2020-08-02 at 7.58.57 AM.png

Votes

Translate

Translate

Report

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 Beginner ,
Jun 08, 2020 Jun 08, 2020

Copy link to clipboard

Copied

@try67.  I am trying to write code for when a checkbox is selected a fee will calculate based on Subtotal and Tax boxes multiplied by .035

So if Checbox7 is selected (Subtotal + Tax) * .035

Votes

Translate

Translate

Report

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 ,
Jun 09, 2020 Jun 09, 2020

Copy link to clipboard

Copied

And if it's not?

Votes

Translate

Translate

Report

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 Beginner ,
Jun 09, 2020 Jun 09, 2020

Copy link to clipboard

Copied

It is just 0 if not.

Votes

Translate

Translate

Report

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 Beginner ,
Jun 09, 2020 Jun 09, 2020

Copy link to clipboard

Copied

I should also clarify this is going into its own box labeled CC Fee.

Votes

Translate

Translate

Report

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 ,
Jun 09, 2020 Jun 09, 2020

Copy link to clipboard

Copied

You can use this code as the custom calculation script of that field:

 

event.value = (this.getField("Checkbox7").valueAsString=="Off") ? 0 : (Number(this.getField("Subtotal").valueAsString) + Number(this.getField("Tax").valueAsString)) * .035;

 

(I assume it's "Checkbox7", and not "Checbox7", as you wrote above)

Votes

Translate

Translate

Report

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 Beginner ,
Jun 09, 2020 Jun 09, 2020

Copy link to clipboard

Copied

That worked beautifully.  Thank you

Votes

Translate

Translate

Report

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
New Here ,
Jun 14, 2021 Jun 14, 2021

Copy link to clipboard

Copied

@try67Hoping you can help me figure this one out; I thought I had it, but it isn't working--I just continue to get a zero value regardless if the checkmark is "yes" or "no".

If the checkbox (circled in red) is checked, then I want it to add the coresponding value (circled in blue are hidden fields with default values matching the hours listed).

My checkboxes are named Core1, Core2, Core3, etc.; my hidden text fields are listed as CoreHours1, CoreHours2, CoreHours3, etc.

Below is a snip of my file as well as a snip of the code I'm trying to use.

Capture1.JPGCapture2.JPG

Votes

Translate

Translate

Report

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 ,
Jun 14, 2021 Jun 14, 2021

Copy link to clipboard

Copied

Post the full code as text, or share the file with us, please.

Votes

Translate

Translate

Report

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
New Here ,
Jun 14, 2021 Jun 14, 2021

Copy link to clipboard

Copied

File attached. Thank you!

Votes

Translate

Translate

Report

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 ,
Jun 14, 2021 Jun 14, 2021

Copy link to clipboard

Copied

You are very close, but the code for forming the number field name is incorrect. Also, this code won't work right now because there aren't enough number fields on the form.  

 

Here's the corrected code:

var total = 0;
for (var i=1; i<=8; i++) {
    if (this.getField("Core"+i).valueAsString=="Yes") {
        total+=Number(this.getField("CoreHours" + i).valueAsString);
    }
}
event.value = total;

  

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

Votes

Translate

Translate

Report

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
New Here ,
Jun 15, 2021 Jun 15, 2021

Copy link to clipboard

Copied

LATEST

That worked! Thank you for your help!!

Votes

Translate

Translate

Report

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