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.

Using parseInt in script but getting NaN result

jtrezza
Kilo Contributor

I am writing a script to add a simple series of form data. I am very new to scripting, but this seemed pretty straight forward. Any thoughts as to what I did wrong?

//this script calculates the total amount of departmental allocations and only allows submit if total value = 100%

function onSubmit() {

var pc1 = parseInt(g_form.getValue('variables.hr_percentage'), 10);
var pc2 = parseInt(g_form.getValue('variables.hr_percentage_2'), 10);
var pc3 = parseInt(g_form.getValue('variables.hr_percentage_3'), 10);
var pc4 = parseInt(g_form.getValue('variables.hr_percentage_4'), 10);
var pc5 = parseInt(g_form.getValue('variables.hr_percentage_5'), 10);
var pc6 = parseInt(g_form.getValue('variables.hr_percentage_6'), 10);

var tot = pc1 + pc2 + pc3 + pc4 + pc5 + pc6 ;

if(tot != '100'){

alert (tot) ;
alert('Total allocation amounts must equal 100%, please adjust the values and re-submit.');
return false;

}else{
return true;
}}

13 REPLIES 13

Your example is correct. I am not sure what was going on a with my original scenario, it was obviously something else.


adiddigi
Tera Guru

Can you please show me an example of what


(g_form.getValue('variables.hr_percentage')
looks like? Does it have % included in it?


jtrezza
Kilo Contributor

Can you please show me an example of what (g_form.getValue('variables.hr_percentage') looks like? Does it have % included in it?

its a variable - single line text.

just numerical value being entered...no spaces or special characters.


Try adding zero like this to see if that forces the correct data type.

var tot = pc1 + pc2 + pc3 + pc4 + pc5 + pc6 + 0;


jtrezza
Kilo Contributor

Mark,
I tried the +0 with the same result.

makes no sense, I am missing something basic I am sure.
next stop "jim Coyne"