3 Replies Latest reply on Feb 3, 2014 7:37 AM by gkaiseril

# Conditional Calculation

Hi All!

I'm creating a fillable module with some calculations. No problem with simple calc with unique fixed values.

The issue is that i have to give a special rule to some calc. I have to calc the total of orders with the price of products, but prices change for multiple orders.

EXAMPLE:

PRODUCTPRICE UP TO 4PRICE FOR 5 OR MOREUNITS ORDERED
TOTAL
APPLE10844*10
ORANGE12933*12
TOMATO7555*5
CARROT151177*11
FISH201822*20

I think i have to write some JS, but I'm not so PRO!!

thank you all for the help

• ###### 1. Re: Conditional Calculation

Let's say in the first row the fields are called Price1, Price2, Units1 and Total1.

Use this code as the custom calculation script of Total1:

var units = Number(this.getField("Units1").value);

if (units<=4) event.value = Number(this.getField("Price1").value) * units;

else event.value = Number(this.getField("Price2").value) * units;

• ###### 2. Re: Conditional Calculation

SORRY FOR CAPS...BUT THANK YOU SO MUCH!!!!

• ###### 3. Re: Conditional Calculation

You will have to write a Custom JavaScript Calculation and for that you need to know each field's name before one can even start. A little fore thought into the fields names might help make the calculation easier.

One could have a column of fields names "Price4", "Price5", "Quantity",. and "Total".

The first row's names would be "Priece4.0", Price5.0", Quantity.0", and "Total.0". The next row would be the ".1" suffix and so on.

One than can use a value to indicate the row being processed and access only the fields in that row.

For the first row's total the script could be"

nRow = 0; // index for row of table.

// get the field values:

var Price4 = this.getField("Pirce4." + nRow).value;

var Price5 = this.getField("Pirce5." + nRow).value;

var Quantity = this.getField("Quantity." + nRow;).value;

// compute extended price on quantity orderd;

if(Quantity > 4) event.value = Quantity * Price5; // quantity of more than 4;

else event.value = Quantity * Price4; // quantity of 4 or less;

One now only needs to copy the code to the other totals and change the value of nRow as needed.