Copy link to clipboard
Copied
I need to create a form to calculate the cost of an item after a discount is applied. However, the discount can be entered as either:
I'm thinking the best way to do this would be to add two radio buttons next to the DISCOUNT input field;
And then the totalCost would use an IF/ELSE statement to calculate using the proper formula.
But I have no idea how to write that calculation. Can anyone help?
Copy link to clipboard
Copied
You can read about writing calculation scripts and "if" statements here:
https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm
https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm
To make this work we have to know the names of the fields involved and the export values of the radio buttons.
I'll just assume some names for the demo script, but you will need to modify it for the real names.
if(this.getField("DiscountType").value == "FlatAmount")
event.value = this.getField("Price").value - this.getField("Discount").value;
else
event.value = this.getFeld("Price).value * (1 - this.getField("Discount").value/100);
Copy link to clipboard
Copied
Correction:
I just realized my percentage calculation should have been... price - (discount/100)price = totalCost
Regardless, I need help writing the field script.
Copy link to clipboard
Copied
You can read about writing calculation scripts and "if" statements here:
https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm
https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm
To make this work we have to know the names of the fields involved and the export values of the radio buttons.
I'll just assume some names for the demo script, but you will need to modify it for the real names.
if(this.getField("DiscountType").value == "FlatAmount")
event.value = this.getField("Price").value - this.getField("Discount").value;
else
event.value = this.getFeld("Price).value * (1 - this.getField("Discount").value/100);
Copy link to clipboard
Copied
Thank you Thom! This worked like a charm. I just cleaned up a few typos...
if(this.getField("DiscountType").value == "FlatAmount")
event.value = this.getField("Price").value - this.getField("Discount").value;
else
event.value = this.getField("Price").value * (1 - this.getField("Discount").value/100);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now