Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Adding sum of integer fields to another field on a form (Client Side)

Inactive_User15
Kilo Contributor

I'm trying to sum multiple integer fields into one on a form. I would like this to be done before the form is saved or submitted AND I need this calculation to trigger based on multiple fields. 

I have about 9 fields that if either one is NOT NULL then I want the calculation to run and populate the sum into a total field. Due to these conditions I'm trying to accomplish this in an UI policy rather than a client script. 

My current code is the following. I've tried using parseInt and adding '+' in front of the integer field. With the code below the field being populate with the total is stating NaN. 

 

function onCondition() {

var appDev = g_form.getValue('u_app_dev_eta_of_hours');
var applServ = g_form.getValue('u_appl_services_emr');
var sumHours = parseInt(appDev) + parseInt(applServ);
g_form.setValue('u_total_hrs_for_prj_rsrc_work',sumHours);
//g_form.setDisplay('u_total_hrs_for_prj_rsrc_work', +u_app_dev_eta_of_hours + 'u_appl_services_emr');


}

13 REPLIES 13

Yogi3
Kilo Guru

What is the type of u_total_hrs_for_prj_rsrc_work? It should be integer.

try this

 

function onCondition() {

var appDev = g_form.getValue('u_app_dev_eta_of_hours');
var applServ = g_form.getValue('u_appl_services_emr');
var sumHours = parseInt(appDev,10) + parseInt(applServ,10);
g_form.setValue('u_total_hrs_for_prj_rsrc_work',sumHours);
//g_form.setDisplay('u_total_hrs_for_prj_rsrc_work', +u_app_dev_eta_of_hours + 'u_appl_services_emr');


}

I also tried adding ,10 as you stated, but when I enter numbers into the fields, the Total Hours field populates with NaN.

Can you please send the screen shot of UI Policy? Also, check if variable names are correct.