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

salemsap
Tera Expert

You can do it using parseInt() function.


For ex.


We have 5 integer fields from field1 to field5.We can add the first four field values and show the calculated value in field5 using below code.



  var a = parseInt(g_form.getValue('field1'));


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


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


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


  var e = a+b+c+d;


  g_form.setValue('field5',e);


Thanks Alex, where would I enter that code, in a client script, and would   it work with both integer and decimal fields?


just create a onchange client script for field4 and copy the code and paste it in script field.


Write befor insert Business rule and populate data.



var a =   current.field1;


  var b = current.field2;


  var c = current.field3;


  var d = current.field4;


  var e = current.field5;


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


  current.fieldname = f;



Regards,


Harish.