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

Calculation Sum if Corresponding Check Box is Marked

New Here ,
Dec 09, 2021 Dec 09, 2021

I am creating an expense reimbursement form. I was able to have the form calculate the sum of the expenses added to multiple boxes, but I would like it to then calculate the total of those boxes based on a corresponding check box. The check box would be used for managers to mark the expense as approved. They may wish to approve some but not all expenses so the total could be different from the total added by the employee. The amounts put into the boxes would very every time so I can just assign a value to the check box. Also, I would like the total to cap out at $150 as that is the max reimbursement amount allowed. I have basically no experience with Java script, but I am assuming that’s what I need to make this work? 

TOPICS
Create PDFs , JavaScript , PDF forms
2.9K
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
Community Expert ,
Dec 09, 2021 Dec 09, 2021

If your fields are named in sequence (Text1-Text5 and checkbox1-checkbox5) you change field names to your actual names since you didn't provide those I used above ones as sample, as 'custom calculation script' of "Total Approved" field try this:

var i, total = 0;
for( i=1; i<=5; i++){
if(this.getField("checkbox"+i).valueAsString != "Off" && typeof this.getField("Text"+i).value === "number")
total += this.getField("Text"+i).value;}
if(total > 150)
event.value = 150;
else event.value = total;

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 ,
Dec 09, 2021 Dec 09, 2021

If your fields are named in sequence (Text1-Text5 and checkbox1-checkbox5) you change field names to your actual names since you didn't provide those I used above ones as sample, as 'custom calculation script' of "Total Approved" field try this:

var i, total = 0;
for( i=1; i<=5; i++){
if(this.getField("checkbox"+i).valueAsString != "Off" && typeof this.getField("Text"+i).value === "number")
total += this.getField("Text"+i).value;}
if(total > 150)
event.value = 150;
else event.value = total;

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
New Here ,
Dec 09, 2021 Dec 09, 2021

Hi, thank you for helping. the amount Boxes are labeled as "amount1" and so on (1-5) and check boxes are labeled as "Check Box1" and so on (1-5). I’m sorry if this is a very rudimentary question but I am copying and pasting the script below? I am also unsure of where to place the script. does it go with the Total Approved box?

 

var i, total = 0;
for( i=1; i<=5; i++){
if(this.getField("checkbox1"+i).valueAsString != "Off" && typeof this.getField("Amount1"+i).value === "number")
total += this.getField("Amount1"+i).value;}
if(total > 150)
event.value = 150;
else event.value = total;

 

 

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 ,
Dec 09, 2021 Dec 09, 2021

Yes,  script goes to Total Approved" field as ' custom calculation script'.

You didn't change to correct field names, you said that fields are labeled "Amount1" and in script you changed to "amount1" same for checkboxes? In script you don't need number next to field name, try like this and check that field names are correct:

var i, total = 0;
for( i=1; i<=5; i++){
if(this.getField("Check Box"+i).valueAsString != "Off" && typeof this.getField("Amount"+i).value === "number")
total += this.getField("Amount"+i).value;}
if(total > 150)
event.value = 150;
else event.value = total;

 

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
New Here ,
Dec 13, 2021 Dec 13, 2021

Hi, I double checked all the field names, and they are correct, but it still doesn't work. when I check a check box the total approved stays $0. Is there anything else I should check?

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 ,
Dec 13, 2021 Dec 13, 2021

Can you share your file here?

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
New Here ,
Dec 13, 2021 Dec 13, 2021

Here it is.

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 ,
Dec 13, 2021 Dec 13, 2021

Your last checkbox field is named "CheckBox6" it should be "CheckBox5".

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
New Here ,
Dec 13, 2021 Dec 13, 2021
LATEST

Thank you so much! 

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