Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

onchange client script based on multiple conditions

daniellethomson
Tera Expert

I have a request to populate a field based on the answer to multiple fields. The script submits without errors but it is showing an error on the catalog item. Below I have included the script and error. Any help on what I'm not seeing would be great. Thank you!

SCRIPT:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == 'registered') {

          return;

    }

    //Type appropriate comment here, and begin script below

   

}function onChange(control, oldValue, newValue, isLoading)

  {

  var title = g_form.getValue('job_title');

  var location = g_form.getValue('location');

  if ('advisor' == 'Yes') {

      g_form.setValue('compliance_group', "Yes");

  }      

  else if (registered == 'Yes'){

      g_form.setValue('compliance_group', "registered");

  }

  else if (registered == "No" && title == "Senior Vice President" || "Managing Director"){

      g_form.setValue('compliance_group',"leadership");

}

  else if (location == "Des Moines, IA") {

      g_form.setValue('compliance_group',"des_moines");

  } else {

      g_form.setValue('compliance_group',"non_registered");

  }

}

ERROR:

onChange script error: ReferenceError: registered is not defined function h_63033715375a22006ca9d5c543990e4b(control, oldValue, newValue, isLoading) { var title = g_form.getValue('job_title'); var location = g_form.getValue('location'); if ('advisor' == 'Yes') { g_form.setValue('compliance_group', "Yes"); } else if (registered == 'Yes'){ g_form.setValue('compliance_group', "registered"); } else if (registered == "No" && title == "Senior Vice President" || "Managing Director"){ g_form.setValue('compliance_group',"leadership"); } else if (location == "Des Moines, IA") { g_form.setValue('compliance_group',"des_moines"); } else { g_form.setValue('compliance_group',"non_registered"); } }

1 ACCEPTED SOLUTION

Since, the same client script is supposed to trigger onchange of 2 or more variables, you should use 'onSubmit' client script as:



function onSubmit() {


  //Type appropriate comment here, and begin script below


  var title = g_form.getValue('job_title');


  var location = g_form.getValue('location');


  if (g_form.getValue('advisor') == 'Yes')


  g_form.setValue('compliance_group', "Advisors");


  else if (g_form.getValue('registered') == 'Yes')


  g_form.setValue('compliance_group', "Registered");


  else if (title == "Senior Vice President" || title == "Managing Director")


  g_form.setValue('compliance_group',"Leadership");


  else if (location == "Des Moines, IA")


  g_form.setValue('compliance_group',"Des Moines, IA");


  else


  g_form.setValue('compliance_group',"Non registered");


}



Screen Shot 2016-10-06 at 8.30.26 PM.pngScreen Shot 2016-10-06 at 8.30.31 PM.png


Let me know, if you face any problem further.


View solution in original post

10 REPLIES 10

Since, the same client script is supposed to trigger onchange of 2 or more variables, you should use 'onSubmit' client script as:



function onSubmit() {


  //Type appropriate comment here, and begin script below


  var title = g_form.getValue('job_title');


  var location = g_form.getValue('location');


  if (g_form.getValue('advisor') == 'Yes')


  g_form.setValue('compliance_group', "Advisors");


  else if (g_form.getValue('registered') == 'Yes')


  g_form.setValue('compliance_group', "Registered");


  else if (title == "Senior Vice President" || title == "Managing Director")


  g_form.setValue('compliance_group',"Leadership");


  else if (location == "Des Moines, IA")


  g_form.setValue('compliance_group',"Des Moines, IA");


  else


  g_form.setValue('compliance_group',"Non registered");


}



Screen Shot 2016-10-06 at 8.30.26 PM.pngScreen Shot 2016-10-06 at 8.30.31 PM.png


Let me know, if you face any problem further.


Also, if 'location' is a Reference field, then, you should use the sys_id of "Des Moines, IA" this record, instead of value.


Deepak- Thank you so much for taking the time to help me. I don't know if I will never be as good as you at scripting. You through that together in minutes, when I've been working on it for days. I will let you know if the script works on our end. Also, thanks for the hint on the sys ID for location- it is a reference field.


Danielle, you are always welcome. I am adding some links for you to refer, if you want to learn scripting in servicenow. These might always be helpful to begin with.


http://wiki.servicenow.com/index.php?title=Creating_a_Catalog_Client_Script#gsc.tab=0


http://wiki.servicenow.com/index.php?title=Useful_Catalog_Scripts#gsc.tab=0


What are some of the available helper functions?


And when nothing works, you can always use this platform.