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

Percentage Calculation based on if Check Box is checked

Community Beginner ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

Hello,

I am trying to do a calculation where there is a 10% discount to the subtotal when the check box is checked, but no discount if the box is not checked. The code that is currently there is what I was given by my professor, but it does not work. Could any experts help me out please? Thank you 

Screen Shot 2021-05-02 at 3.51.37 PM.png

TOPICS
JavaScript , PDF forms

Views

1.4K

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
community guidelines
Community Expert ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

Did your professor gave you this code as a homework to find out what is wrong with it?

 

For starters, it looks like you copied the code into the JavaScript editor from the Internet or another program.

 

The use of all the quotes in that script are not the correct fontype (or typeface). This will posibbly throw some  errors.

 

The other thing, is that you need to be more specific and explain what exactly is the intent of the script as it was explained to you in class.

 

You should use these type of quotes "", for example.

 

Also, there are different ways of achieving the same results with JavaScript and built-in features.

 

So where is this executing from? From the checkbox field object or one of the text fields as a custom calculating script?

Votes

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
community guidelines
Community Beginner ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

Thanks for responding.

No, the professor provided the code as part of a guide and said that we could just copy-paste it into the custom calculation dialogue box. They did not go into further detail after "discount" showed the default value of $0.00. I addressed it last class, but they told us they don't know, so my entire class and I have been trying to find a solution. 

I'm sorry for not being specific enough. The screenshot has the script executing from the "discount" text field. The checkbox that is next to it is named "discountCheck". What we are required to achieve is when the checkbox "discountCheck" is selected, the text field "discount" will show 10% of the value from "subtotal", but as a negative value. For example, if "subtotal" showed a value of $100.00, selecting the "discountCheck" checkbox will result in "discount"  having a value of -$10.00. 

If "discountCheck" is unselected, then "discount" will have a value of $0.00. I hope this makes more sense.

Votes

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
community guidelines
Community Expert ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

++ EDITED REPLY,  found some errors in the example script

 

 

If I understood correctly, the custom calculation script will be used in the checkbox field, and will execute the calculation when the checkbox is ticked and display the resulting total in the "discount" field, correct?

 

See script example below , always declare your variables first on top of the script. 

Note that variable "f" in my example is used to listen for the checked state of the checkbox, in which case, "Yes"  will be used as the export value of this checkbox when it is ticked on.

Var f will also listen to the unchecked state of this checkbox, in which case, "Off"  will be used as the export value when the checkbox is ticked off. 

So again, in the example below, the script will execute from the checkbox field object, which is our event target field (or the field where this script will execute if the desired conditions are met. )

Our conditions will consist of the product  that will be obtained from the value in the  "subtotal" field multiplied by the desired percentage (.10).

 

The total product will be  populated automatically in the  discount "field" when the checkbox is ticked on, and will display a value of zero (0) if the checkbox is ticked off.

 

 

 

var f = even.target;
var subtotal = this.getField("subtotal").value;

if (f.value !== "Off") {

this.getField("discount").value = subtotal - (subtotal * .10);

} else {

this.getField("discount").value = 0;

 }

}

 

 

 

NOTE: Like I said before, this can be done in many ways using Acrobat JavaScript. See if my example script works. I am texting from a mobile device and I haven't tested it yet. Let me know if it helps or if it is throwing any errors.

Votes

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
community guidelines
Community Beginner ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

The last curved bracket gave me an error. 

And yes, you got what I was trying to say. Another question, sorry if it is dumb: Should this custom calculation go under the checkbox properties (instead of the "discount" text field properties)? 

Thank you again

Votes

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
community guidelines
Community Expert ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

Hi guys, just to add, your professor code should work, please check that your checkbox name is correct and that it has export value of "Yes", also if you want it to be negative value, change last line to this:

event.value = discount-(discount*2);

@ls_rbls In your code last bracket is an extra and in first line you are missing 't' in event, your code will show subtotal minus discount, I belive OP only needs discount to show 🙂

Votes

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
community guidelines
Community Expert ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Thank you as usual NesaNurani for spotting that!

 

I initially thought of that, but I edited my reply based on what OP explained here:

 

"subtotal", but as a negative value. For example, if "subtotal" showed a value of $100.00, selecting the "discountCheck" checkbox will result in "discount"  having a value of -$10.00. "

 

And yes,  I didn't emphasize clear enough that I intended the example script to be run the checkbox itself, not from the discount field.

 

But you're right, the original code should work if run from the discount field. I can't try this from my mobile device.

 

 

Votes

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
community guidelines
Community Expert ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Such a simple operation does not require Javascript.

 

- Assign "0.10" as the export value to the Discount-checkbox

- Assign this calculation to the Discount text field: Subtotal x Discount-checkbox

 

Votes

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
community guidelines
Community Expert ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Yes, absolutely... I thought of that too but then again is a homework that the professor gave the students and he/she wanted the students to figure out the script that was given in class.

Votes

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
community guidelines
Community Beginner ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

@ls_rbls @JR Boulay @Nesa Nurani I really appreciate all of you. Thank you. For some reason, the original code wasn't changing anything, possibly because there was no mention of changing the export value in my professor's guide? I'm not sure. 

Votes

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
community guidelines
Community Expert ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

LATEST

I think that the original script can work as a custom calculation script in the discount field as mentioned by NesaNurani, in which case there's no need to assign an export value because the script already sets the condition based on the Off and Yes status of the checkbox.

 

But because the default status of checkboxes is usually set to Off ( or unchecked), I would've expressed it slightly different.

 

But to be on the safe side, if the original script isn't working in the discount field, hit CTRL + J  key combination in your keyboard to open the debugger console and see if you're getting any errors.

 

If you see any errors take another screenshot and report it back to us.

 

In my opinion, I think the problem is in declaring the percentage variable with a value of 0 and then changing the value of that variable to .10 via script.

 

This variable, as noted by JRBoulay, doesn't need to be declared at all.

 

You can use the same original script in tje discount field like:

 

if (this.getField("discountCheck").value == "Yes") 

 event.value = this.getField("subtotal").value * .10;

 

else event.value = 0;

 

 

 

Votes

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
community guidelines