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

veena_kvkk88
Mega Guru

I can see where you are defining the variable 'location' but can't find definitions for 'registered' or 'advisor'. Also, in the first if condition, you are comparing two different strings which will never return 'true'.


deepakgarg
ServiceNow Employee
ServiceNow Employee

It seems you are using it in wrong way. I dont know what are you trying to achieve exactly, but try this:



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


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


          return;


    }


  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");


  }


}



Also, post a screenshot of your script if it doesn't work for you. Also, explain your use case if possible.


Also, as Veena mentioned, first if condition will never be true.


if ('advisor' == 'Yes')



Also, you forgot to define variable "registered".



Also, it should be:


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



If you need some help with scripting, please explain your use case.


daniellethomson
Tera Expert

As you probably figured I'm still very green in the world of scripting! I apologize for not providing more detail. Our Compliance department has requested to have 2 fields added to our new hire form. Based on the selection of these two fields and a user's title and/or location another field called compliance_group will be auto populated.



Two fields:


Will the individual be an associated person of COMPANY XYZ Investment Advisors? Label is advisor


Will the individual be an associated person or registered representative of the company's broker/dealer? Label is registered




Script needs to do the following:


  • If the question   "Will the individual be an associated person of COMPANY XYZ Investment Advisors?" is yes, then compliance_group should be Advisors. This rule is at the highest level and cannot be overridden.
  • If the question "Will the individual be an associated person or registered representative of the company's broker/dealer?" is yes, then compliance_group should be registered unless the advisor question is yes.
  • Else if title = Senior Vice President or Managing Director then compliance_group Leadership
  • Else if location = Des Moines, IA then compliance_group Des Moines
  • Else Add user to compliance_group Non Registered