Creating a calculated field on a form based on other values

Posted by Rollbase User on 27-Mar-2012 10:34

If I have a page with two fields 'quantity' and 'unit price' and I want to display another field when both of these are completed called 'total price' which is the product of the previous two how can I achieve this on the page. I could use a status change and use a trigger to calculate the field but don't really want to do this (not as friendly). I'd like it to happen when automatically when the other fields are completed. This is probably described somewhere, but I've missed it.

All Replies

Posted by Admin on 27-Mar-2012 11:17

Hi,



You actually don't need status change and a trigger if computation is on the fly. On object definition, set onchange and probably onkeypress of quantity and unit price to a function probably named compute().



Within the compute function in page, you could use client side api and JavaScript,

Ex.

function compute() {

var total = parseFloat( rbf_getFieldValue('quantity') ) * parseFloat( rbf_getFieldValue('unit_price') );



rbf_setFieldValue('total_price',total.toFixed(2) );

}



You'll probably need more complex scripts that this where you can leverage the Rollbase api and Js or jquery and other techniques.



You can read more on documentation on client side api in chapter 7 I believe.



Piscoso , Martin

Rollbase ph

Posted by Admin on 27-Mar-2012 12:34

Chapter 7 has an example - please take a look.

Posted by Admin on 27-Mar-2012 12:59

Great suggestion Martin!



Paul, Martin has it right -- you'll want to add some custom JavaScript to your page to do this. You'll probably want to do the same thing on both the New and Edit pages for that object. If you can't make progress using this suggestion/approach let us know and we can dig in further to help.

Posted by Admin on 28-Mar-2012 03:00

Yes, thanks all, I've found the examples.

This thread is closed