4 Replies Latest reply on Jan 24, 2017 7:35 AM by Karl Heinz Kremer

# Custom Calculation Script to apply a conditional discount

I am trying to figure out how to write a custom calculation script for a discount on an order form.

There are several items on the order form (all \$16.60 each).

I add all the items and multiply them by 16.60 for the TOTAL.

If the TOTAL is greater than \$398.40 (= 24 items), then I want to give a new total that takes 5% off the total.

Can someone please help me create that formula? I have no idea how to make these javascript formulas. Thank you!!!

• ###### 1. Re: Custom Calculation Script to apply a conditional discount

I probably would not just change the total without indicating why the total is no longer a multiple of \$16.60 - I would have a filed called "SubTotal" (which is the total before the discount), and then another field in which I would calculate the discount, and then the new "Total" field that is the SubTotal - Discount.

In the Discount field, you can use something like this as your calculation script:

```var subTotal = this.getField("SubTotal").value;

event.value = 0; // the default for this field
if (subTotal > 398.40) {
event.value = (subTotal * 0.05).toFixed(2);
}
```

The script for "Total" would then be this:

```event.value = this.getField("SubTotal").value - this.getField("Discount").value;
```
• ###### 2. Re: Custom Calculation Script to apply a conditional discount

If you really want do this with just one Total field, use this:

```event.value = Number(this.getField("Text1").value) + Number(this.getField("Text2").value) +
Number(this.getField("Text3").value) + Number(this.getField("Text4").value); // keep on going until you have all your input fields

if (event.value >  398.40) {
event.value = (event.value * 0.95).toFixed(2);
}
```
• ###### 3. Re: Custom Calculation Script to apply a conditional discount

Can you help me understand which field to put this first calculation script?

1. var subTotal = this.getField("SubTotal").value;
2.
3. event.value = 0; // the default for this field
4. if (subTotal > 398.40) {
5.     event.value = (subTotal * 0.05).toFixed(2);

I have a field for:

total qty items

subtotal (qty*16.60)

and total (event.value = this.getField("SubTotal").value - this.getField("Discount").value;  )

• ###### 4. Re: Custom Calculation Script to apply a conditional discount

As I said, this would be an additional field called "Discount". So in in addition to the "Total" and "SubTotal" fields you mention, you would need to add a third field called "Discount" and use the script from above.