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

Community Beginner ,
Jun 08, 2020

Copy link to clipboard

Copied

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 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;

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

492

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 and multiply by a fixed number if a checkbox is selected.

Community Beginner ,
Jun 08, 2020

Copy link to clipboard

Copied

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 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;

TOPICS
Acrobat SDK and JavaScript, How to, PDF forms

Views

493

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

Copy link to clipboard

Copied

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.

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
Adobe Community Professional ,
Jun 08, 2020

Copy link to clipboard

Copied

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.

 

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

Copy link to clipboard

Copied

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.

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

Copy link to clipboard

Copied

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.

 

 

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

Copy link to clipboard

Copied

Read my reply again. No self contradiction.

 

Your code has an "ELSE IF" statement. I stated that an "ELSE IF" was not needed. Yes, my code is using an "using an IF and ELSE statement...", but it DOESN'T use an "ESLE IF" statement.

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

Copy link to clipboard

Copied

Good catch!

 

I wasn't looking at my typo.

 

Now I feel like an idiot.

 

Thank you for spotting that.

 

 

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
Participant ,
Jun 10, 2020

Copy link to clipboard

Copied

Nah, don't sweat it! I look like an idiot all the time! We all come here to ask for help and to offer help. That's what's important.

 

Have a great Day!

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 10, 2020 1
Community Beginner ,
Jun 17, 2020

Copy link to clipboard

Copied

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;

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 17, 2020 1