Highlighted

Sum numeric fields based on selected checkboxes

Community Beginner ,
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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

1.2K

Likes

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

Sum numeric fields based on selected checkboxes

Community Beginner ,
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.

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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;

TOPICS
Acrobat SDK and JavaScript, Windows

Views

1.2K

Likes

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
Feb 18, 2017 0
Most Valuable Participant ,
Feb 19, 2017

Copy link to clipboard

Copied

What are the names of the check-box fields?

Likes

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
Reply
Loading...
Feb 19, 2017 0
Community Beginner ,
Feb 19, 2017

Copy link to clipboard

Copied

E101

E102

E104

E105

E107

F102

F104

Likes

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
Reply
Loading...
Feb 19, 2017 0
Most Valuable Participant ,
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;

Likes

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
Reply
Loading...
Feb 19, 2017 2
Community Beginner ,
Feb 19, 2017

Copy link to clipboard

Copied

Thanks. Really simple yet elegant code. 🙂

Likes

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
Reply
Loading...
Feb 19, 2017 0
New Here ,
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;

 

Likes

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
Reply
Loading...
Feb 25, 2020 0
Most Valuable Participant ,
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;

Likes

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
Reply
Loading...
Feb 26, 2020 0
New Here ,
Feb 26, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Feb 26, 2020 0
New Here ,
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!!! 

Likes

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
Reply
Loading...
Apr 16, 2020 0
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Apr 16, 2020 0
New Here ,
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

Likes

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
Reply
Loading...
May 28, 2020 0
New Here ,
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?

Likes

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
Reply
Loading...
Jul 31, 2020 0
Most Valuable Participant ,
Jul 31, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jul 31, 2020 0
New Here ,
Aug 02, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Aug 02, 2020 0
Community Beginner ,
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

Likes

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
Reply
Loading...
Jun 08, 2020 0
Most Valuable Participant ,
Jun 09, 2020

Copy link to clipboard

Copied

And if it's not?

Likes

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
Reply
Loading...
Jun 09, 2020 0
Community Beginner ,
Jun 09, 2020

Copy link to clipboard

Copied

It is just 0 if not.

Likes

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
Reply
Loading...
Jun 09, 2020 0
Community Beginner ,
Jun 09, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jun 09, 2020 0
Most Valuable Participant ,
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)

Likes

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
Reply
Loading...
Jun 09, 2020 1
Community Beginner ,
Jun 09, 2020

Copy link to clipboard

Copied

That worked beautifully.  Thank you

Likes

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
Reply
Loading...
Jun 09, 2020 0