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

And you have written g_form.setValue('u_total_days', g);


this line also need to replace .



G-form is client side object it doesnt work in server side.



Regards,


Harish.


Harish,



Thanks for you answer above. I found it but cannot seem to get it to work. Could you help me?



This is what I have:



var gpScore = current.gpra_score;
var gpSup = current.gpra_supplemental;
var gpAdj = parseInt(gpScore) + parseInt(gpSup);
gpAdj = current.gpra_adjusted_score;


the two fields being added are both choice fields and the field I'm dropping the variable into is a string. Not sure if that matters, but I've tried a few variations and it doesn't seem to be working for me.



Thanks,


w


wwc
Kilo Explorer

I figured it out. The Choice field was using decimals but parseInt() only treated them as strings.


this is the correct answer. thanks hmurikinati


A business rule would definitely be the way to go here, but there's a difference in getting the value of a field between server and client side scripting. Basically both of the following will return the same value respective to where they're running:



Client (Client Scripts/UI Policies)--> g_form.getValue('fieldname');


Server (Business Rules/Script Includes) --> current.fieldname