Client Script for adding values from 3 fields and populating it on other field

sachinshivakuma
Mega Contributor

Hi,

I need some help scripting on adding values from 3 fields and populating them in a new field.

Thanks in advance.

19 REPLIES 19

TrevorK
Kilo Sage


var total_cost_annual = 0;


total_cost_annual = parseFloat(g_form.getValue("u_annual_hw_costs.display").replace(/,/g,'')) + parseFloat(g_form.getValue("u_annual_sw_costs.display").replace(/,/g,'')) + parseFloat(g_form.getValue("u_annual_labour_costs.display").replace(/,/g,''));



g_form.setValue("total_cost",total_cost_annual);



So what we are doing:


a) We have a variable, total_cost_annual


b) We are getting the values from 3 fields through a g_form.getValue command


c) We are using a .replace(/,/g,'') to remove the comma (you only need to do this if you care - this code is for currency fields which use commas and parseFloat does not deal with commas)


d) We are setting the value to the field total_cost



You will need to put the output in whatever the field type is (mine is float in this case).



Perhaps provide a screen shot of what each of the three field types you are adding are (int, decimal, float) and the field you want to calculation to go to and we can give the exact commands.


Chuck Tomasi
Tera Patron

Are you adding integer values or want to concatenate strings?



For strings: it would look something like this



var f1 = g_form.getValue('field1');


var f2 = g_form.getValue('field2');


var f3 = g_form.getValue('field3');



g_form.setValue('field4', f1 + ' ' + f2 + ' ' + f3);



for numbers, you want to ensure they are converted to integers first



var f1 = parseInt(g_form.getValue('field1'), 10);


var f2 = parseInt(g_form.getValue('field2'), 10);


var f3 = parseInt(g_form.getValue('field3'), 10);



var answer = f1 + f2 + f3;



g_form.setValue(field4, answer);


Hi Chuck,



Adding integer values...



Thanks


Use the second part then. The key is to remember to use parseInt() to convert strings to numeric values, otherwise you just concatenate them.



3 + 10 + 5 ends up being 3105 without parseInt().