Need to SUM the contents of several fields

amacqueen
Mega Guru

I don't know if this is possible but lets say I have 5 fields on a form each of which are called field 1 to 5 respectively and I want to add them and put the sum in field 6.

I've seen the WIKI Article GlideAggregate - ServiceNow Wiki but don't know if that will work for me as I'm a javascript idiot.

Does anyone have any suggestions as to how best I can approach this please?

TIA

1 ACCEPTED SOLUTION

Copy paste this code and check




var a = parseInt(current.u_initiation_days_);


var b = parseInt(current.u_development_days_);


var c = parseInt(current.u_configuration_days_);


var d = parseInt(current.u_testing_uat_days_);


var e = parseInt(current.u_pm_days_);


var f = parseInt(current.u_external_days);




var g = parseInt(a)+parseInt(b)+parseInt(c)+parseInt(d)+parseInt(e)+parseInt(f);




gs.log('Total :'+g);



current.u_total_days = g;


View solution in original post

19 REPLIES 19

Thanks Alex I've created an onChange client script on the correct table and with the field name selected asa the same field as the g)form.setValue field but it doesn't work for me. Can you see any errors with my script (the javascript validator accepts it)



function onChange(control, oldValue, newValue, isLoading, isTemplate) {  
if (isLoading || newValue == '') {  
    return;  
}  
  var a = parseInt(g_form.getValue('u_initiation_days_'));


  var b = parseInt(g_form.getValue('u_development_days_'));


  var c = parseInt(g_form.getValue('u_configuration_days_'));


  var d = parseInt(g_form.getValue('u_testing_uat_days_'));


  var e = parseInt(g_form.getValue('u_pm_days_'));


  var f = parseInt(g_form.getValue('u_external_days'));


  var g = a+b+c+d+e+f;


  g_form.setValue('u_total_days',g);
}  




Thanks


code seems good.check the field names.


i Hope all fields are integer and you have created onchange script of u_external_days field.


Anurag Tripathi
Mega Patron
Mega Patron

Hi Angus,



I had a similar requirement, i used "calculated" fields feature and it works perfect. Let me know if you need any information on that.


-Anurag

Hi Anurag,

How to do for demical value for calculated filed.

Like i need to calculate total hours from child table and put that sum value in parent.When i am giving decimal values the sum is not calculated correctly.

var childTable = 'Resource_plan';// from which table you want to make sum
var numberField = 'planned_hours'; // what do we want to sum up?
var count = 0;
var gr = new GlideRecord(Resource_plan);
gr.addQuery('task', current.sys_id);
gr.query();
while (gr.next()) {
var n = parseInt(gr.getValue(numberField), 10);
count += n;

}