How can we use Dynamic Calculation field

Posted by Sudhakar on 29-Jul-2014 10:54

Hi all,

how can we use dynamic calculation(EX:: if i enter value 1 in field1  and 2 in field2 then it should sum both number and display 3 in fieldd 3,i know that we can use formula fields but we need to see the result before saving the record.i.e Dynamic calculation field).

any one pls help me how can i do the above.

All Replies

Posted by Godfrey Sorita on 29-Jul-2014 11:28

Hi Sudhakar,

You can use Rollbase client-side APIs to compute the total dynamically.

For example, you have 'num1', 'num2', 'num3' and 'sumField' integer fields on your form. To compute the sum, paste the code below on a script component on your form's page editor:

<script>
function computeSum() {
var num1 = rbf_getFieldValue('num1'),
  num2 = rbf_getFieldValue('num2'),
  num3 = rbf_getFieldValue('num3'),

  sum = parseInt(num1) + parseInt(num2) + parseInt(num3);

  rbf_setFieldValue('sumField', sum);
}
</script>

Then, add the computeSum() function to the onchange events of 'num1', 'num2' and 'num3' fields. This will compute for the total when any of the three fields are changed.

Please see Chapter 7 of Rollbase in Action

Regards,

Godfrey

Posted by jmehta@allwestern.com on 30-Jul-2014 13:54

Hello,

I tried this one but i am not able to get the output.

Here is the CODE.

<script>

function computeSum() {

var num1 = rbf_getFieldValue('interest_rate'),

 num2 = rbf_getFieldValue('term'),

 num3 = rbf_getFieldValue("amount'),

var rate = num1/(100*12);

var r = rate + 1;

var calc = 1-(Math.pow(r, - num2));

var p = (rate/calc) * num3;

p = p.toFixed(2);

if (p == "Infinity" || p == "NaN") return 0;

else {

rbf_setFieldValue('payment',p);}

}

</script>

NOTE:

1. interest_rate is percentage field.

2. term is radio button field.

3. amount is Currency field.

Please check it and Help to solve this.

Thanks

Jigs Mehta

Posted by Godfrey Sorita on 30-Jul-2014 14:41

Hi Jigs,

I've noticed a few errors from from your code. Please see my comments below:

  1. num3 = rbf_getFieldValue("amount'),
    - change the first double quote into a single quote.
    - change the comma into a semi-colon.
  2. num2 = rbf_getFieldValue('term'),
    - In getting the value of picklists or radio buttons, I would suggest using the rbf_getPicklistCode API.
    - Assign numeric radio button option codes to your 'term' field to make rbf_getPicklistCode work. 
    - Make sure to use the 'replace' action when updating the value of your radio button.
  3. If you're using Google Chrome as your browser, press F12 and navigate to the console section. It should display the errors in your current page. 

Kindly also post the error message from the console ,if any, so I can assist you more effectively on this issue.

Regards,

Godfrey

This thread is closed