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

jtrezza
Kilo Contributor

So Mark the +0 was the key, but it needed to be in the beginning of the parseInt call to force any blanks in the individual numbers to a zero.
Coyne to the rescue....



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

function onSubmit() {

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


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

if(tot != 100){


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

}else{
return true;

}}


This is what happens when your co-workers can't get a hold of you.... 🙂


Hi ,



Can any one help me on the below one.



I am adding two variables which are single line text in catalog item and after the closed completeion of task in Run script activity of workflow i am using the below code.



but while adding two variables and storing them in third variable, I am not getting the exact result. Instead of creating a record it is updating the record.



var primaryKeyCheck = current.variable_pool.Company_Number + current.variable_pool.server;


primaryKeyCheck = primaryKeyCheck.toString();


gs.log('Primary Key', + primaryKeyCheck);


var ciRcd = new GlideRecord('u_baan_site_contact_matrix');


ciRcd.addQuery('u_company_number_and_erp_prima',primaryKeyCheck);


ciRcd.query();


if(ciRcd.next())


  {


  updateCommonFields(ciRcd, current);


  ciRcd.update();


}


else


{ ciRcd.initialize();


  updateCommonFields(ciRcd, current);


  ciRcd.insert();


  }


function updateCommonFields(ciRcd, item){


  if (item.variable_pool.Company_Number!= '') { //Company number


  ciRcd.u_baan_company = item.variable_pool.Company_Number;


}


if (item.variable_pool.package_Com != '') {


  ciRcd.u_package_combination = item.variable_pool.package_Com;


}


if (item.variable_pool.Network_Site_Code != '') {


  ciRcd.u_location__network_internal_s = item.variable_pool.Network_Site_Code;


}


if (item.variable_pool.Glb_Entity_Mgment_System != '') {


  ciRcd.u_global_entity_management_sys = item.variable_pool.Glb_Entity_Mgment_System;


}


if (item.variable_pool.Month_End_Package != '') {


  ciRcd.u_month_end_package__mep__code = item.variable_pool.Month_End_Package;


}


if (item.variable_pool.Site_Project_Leader != '') {


  ciRcd.u_site_it_contact_1 = item.variable_pool.Site_Project_Leader;


}


if (item.variable_pool.site_manager != '') {


  ciRcd.u_site_it_manager = item.variable_pool.site_manager;


}


if (item.variable_pool.Site_Financial_Controller != '') {


  ciRcd.u_site_finance_controller = item.variable_pool.Site_Financial_Controller;


}


if (item.variable_pool.Site_Gral_Mngr != '') {


  ciRcd.u_site_general_manager = item.variable_pool.Site_Gral_Mngr;


}


if (item.variable_pool.emaildist != '') {


  ciRcd.u_site_contact_dl = item.variable_pool.emaildist;


}


if (item.variable_pool.gdhm_site != '') {


  ciRcd.u_site_contact_dl = item.variable_pool.gdhm_site;


}


if (item.variable_pool.Other_Instructions_or_Comments_2 != '') {


  ciRcd.u_comments = item.variable_pool.Other_Instructions_or_Comments_2;


}


if (item.variable_pool.Finance_Company_Number != '') {


  ciRcd.u_finance_company_no__finco_ = item.variable_pool.Finance_Company_Number;


}


if (item.variable_pool.ERP_existing_system != '') {


  ciRcd.u_existing_erp_system = item.variable_pool.ERP_existing_system;


}


if (item.variable_pool.ERP_system != '') {


  ciRcd.u_new_erp_system = item.variable_pool.ERP_system;


}


if (item.variable_pool.gicoord != '') {


  ciRcd.u_gi_coordinator_for_this_impl = item.variable_pool.gicoord;


}


if (item.variable_pool.If_Yes__include_comp_num2 != '') {


  ciRcd.u_table_sharing_company_no_1 = item.variable_pool.If_Yes__include_comp_num2;


}


if (item.variable_pool.site_physical_address != '') {


  ciRcd.u_complete_site_physical_addre = item.variable_pool.site_physical_address;


}


if (item.variable_pool.Legal_Entity_Name != '') {


  ciRcd.u_exact_legal_entity_name = item.variable_pool.Legal_Entity_Name;


}


if (item.variable_pool.xDSS_Code != '') {


  ciRcd.u_xdss_code = item.variable_pool.xDSS_Code;


}


if (item.variable_pool.purchasing_manager != '') {


  ciRcd.u_purchasing_manager = item.variable_pool.purchasing_manager;


}


if (item.variable_pool.identify_country != '') {


  ciRcd.u_country = item.variable_pool.identify_country;


}


if(item.variable_pool.Company_Number != '')


  {


  ciRcd.u_company_number_and_erp_prima = item.variable_pool.Company_Number + item.variable_pool.server;


}


}


Harish Murikina
Tera Guru

Hi jtrezza,




Try this code it can works.




var pc1 = g_form.getValue('variables.hr_percentage');


var pc2 = g_form.getValue('variables.hr_percentage_2');


var pc3 = g_form.getValue('variables.hr_percentage_3');


var pc4 = g_form.getValue('variables.hr_percentage_4');


var pc5 = g_form.getValue('variables.hr_percentage_5');


var pc6 =g_form.getValue('variables.hr_percentage_6');




var tot = parseInt(pc1) + parseInt(pc2) + parseInt(pc3) + parseInt(pc4) + parseInt(pc5) + parseInt(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;


}




Regards,


Harish.