Skip to main content
Averie221769287i7j
Participant
December 9, 2021
Answered

Calculation Sum if Corresponding Check Box is Marked

  • December 9, 2021
  • 1 reply
  • 2864 views

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? 

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

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;

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
December 9, 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;

Averie221769287i7j
Participant
December 9, 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;

 

 

Averie221769287i7j
Participant
December 13, 2021

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


Thank you so much!