Skip to main content
June 9, 2020
Answered

Sum numeric fields and multiply by a fixed number if a checkbox is selected.

  • June 9, 2020
  • 3 replies
  • 2497 views

I am trying to write calculation 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

This topic has been closed for replies.
Correct answer

This is what I ended up using.  :  answer came from try67.  event.value = (this.getField("Checkbox7").valueAsString=="Off") ? 0 : (Number(this.getField("Subtotal").valueAsString) + Number(this.getField("Tax").valueAsString)) * .035;

3 replies

Correct answer
June 17, 2020

This is what I ended up using.  :  answer came from try67.  event.value = (this.getField("Checkbox7").valueAsString=="Off") ? 0 : (Number(this.getField("Subtotal").valueAsString) + Number(this.getField("Tax").valueAsString)) * .035;

ls_rbls
Community Expert
Community Expert
June 9, 2020

You may want to implement an IF /ELSE condition with your checkbox.

 

You can use something like this:

 

if (this.getField("Checbox7").value =="Yes"){
event.value = (this.getField("Subtotal").value+ this.getField("Tax").value)*.035;
}

else if (this.getField("Checbox7).value =="Off"){
event.value = this.getField("Subtotal").value + this.getField("Tax").value;
}

 

 

 

You can use this script as the custom calculation script  of your Total field, for example.

 

Old_Salt
Participating Frequently
June 9, 2020

There are several ways to skin this cat. No else if is needed. It either == "Yes" or it doesn't. I used event.target.value since I was applying the mouse up action to the checkbox.

ls_rbls
Community Expert
Community Expert
June 10, 2020

You just contradicted yourself.

 

Your line of code above is actually using an IF  and ELSE statement...

 

So, I don't know what you're talking about.

 

 

Old_Salt
Participating Frequently
June 9, 2020

Assuming SubTotal is calculated in a SubTotal field using a sum calculation applied to the SubTotal field that adds a number of number fields.

And assuming the tax is calculated as a percentage of the subtotal or a number entered into a Tax Field. I used a flat percentage based on location tax rates.

 

Try the following by creating a mouse up javascript assigned to the checkbox:

//------------------------------------------------------

var st = this.getField("SubTotal").value;
var tax = this.getField("Tax").value;
var total = this.getField("Total").value;

if (event.target.value == "Yes") { // This is the checkbox 
total = (st+tax)*1.035;
} else {
total = st+tax;
}
this.getField("Total").value = total;

//-------------------------------------------------------

I've tested this and it works for me.