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

find_real_file.png

find_real_file.png

what are the value on u_appl_services_emr and u_app_dev_eta_of_hours. Highly likely they might have letters instead of numbers as text

They are both integer fields. As I'm testing I'll enter a couple numbers in each which triggers the policy and results in NaN. 

try running your code in a function. It might be taking values from other UI policies. Use below code.

 

function onCondition() {

setHours();

function setHours() {
var appDev1 = g_form.getValue('u_app_dev_eta_of_hours');
var applServ1 = g_form.getValue('u_appl_services_emr');
var sumHours1 = parseInt(appDev1,10) + parseInt(applServ1,10);
g_form.setValue('u_total_hrs_for_prj_rsrc_work',sumHours1);

}

}

It is an integer