Hide Question Choice in Select Box variable

nicolemccray
Tera Expert

If the user indicates 'request_for_new_hire = No, I want to hide the 'New Hire' option in the 'employee_type' select box.  My script is not working:

 

function onChange(control, oldValue, newValue, isLoading) {
 
 
 if (isLoading || newValue == '') {
  
  
  
  return;
 }
 
 //Type appropriate comment here, and begin script below
 var reqFor = g_form.getValue('request_for_new_hire');
 
 
 if(reqFor == 'No'){
  
  
  g_form.removeOption('employee_type', 'New Hire');
  
  
 }
 
 
 else{
  
  
  g_form.addOption('employee_type', 'New Hire');
  
  
 }
 
}

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Maybe you also want to change the values of your choices, it's not best practice to use spaces, stripes, etc.. So use new_hire, non_employee instead.

Just tested with code below, works fine:

function onChange(control, oldValue, newValue, isLoading) {
	
	if(isLoading || newValue == '') {
		return;
	}
	
	var reqFor = g_form.getValue('request_for_new_hire');
	
	if(reqFor == "No") {
		g_form.removeOption('employee_type', 'new_hire');
	} else {
		g_form.addOption('employee_type', 'new_hire', 'New hire');
	}

}

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

5 REPLIES 5

Alikutty A
Tera Sage

Hi,


Is New Hire the label or value of your choice list? The remove Option should be provided with actual value of the option.

 

Thanks!

The variable is 'employee_type', and these are the options and values:

 

find_real_file.png

Have you written the on change script on variable request_for_new_hire? Also what is the actual value for request_for_new_hire.

Mark Roethof
Tera Patron
Tera Patron

Maybe you also want to change the values of your choices, it's not best practice to use spaces, stripes, etc.. So use new_hire, non_employee instead.

Just tested with code below, works fine:

function onChange(control, oldValue, newValue, isLoading) {
	
	if(isLoading || newValue == '') {
		return;
	}
	
	var reqFor = g_form.getValue('request_for_new_hire');
	
	if(reqFor == "No") {
		g_form.removeOption('employee_type', 'new_hire');
	} else {
		g_form.addOption('employee_type', 'new_hire', 'New hire');
	}

}

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn