Modify choices on a list based on another field, but I get onChange script error: ReferenceError: g_list is not defined function () { [native code] }

Kub
Tera Expert

Hello, I'm working on an onChange client script. I have a requirement to limit choices on a list type element based on another field called 'role'. 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var role = g_form.getValue('role');

	var responsibility = g_list.get("responsibility_template");

   //Remove the unnecessary choices

	if(role == 'architect'){
		responsibility.removeOption('low');
	}
	
	if(role == 'developer'){
		responsibility.removeOption('high');
	}
   
}

It seems that g_list throws an error

find_real_file.png

I also tried an approach with GlideList2.getByName() and GlideList3.getByName(), but I get similar error, that GlideList3 is not defined.

Do you have any idea how to fix this error? 

1 ACCEPTED SOLUTION

Thank you for the reply, 

I managed to work around this issue by using dependent field on 'responsibility_template' dictionary.
Here's the video I used so that somebody might need it in the future.

 

http://www.john-james-andersen.com/blog/service-now/creating-dependent-choice-lists-in-servicenow.html

Lesson for today, g_list doesn't work in client scripts

View solution in original post

9 REPLIES 9

Okay you are having list field with choices

I doubt you can achieve this directly using any ServiceNow methods

I tried this and it worked for me but it would remove the option only when the option is already selected

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

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

	var selectobject = this.document.getElementById("select_0" + g_form.getTableName() + ".responsibility_template");


	if(role == 'architect'){
		for (var i=0; i<selectobject.length; i++) {
			if (selectobject.options[i].value == 'low')
				selectobject.remove(i);
		}
	}
	if(role == 'developer'){
		for (var i=0; i<selectobject.length; i++) {
			if (selectobject.options[i].value == 'high')
				selectobject.remove(i);
		}
	}

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Kub 

Using DOM is not recommended

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Kub 

Thank you for marking my response as helpful.

if that helped please mark it correct and close the thread.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you for the reply, 

I managed to work around this issue by using dependent field on 'responsibility_template' dictionary.
Here's the video I used so that somebody might need it in the future.

 

http://www.john-james-andersen.com/blog/service-now/creating-dependent-choice-lists-in-servicenow.html

Lesson for today, g_list doesn't work in client scripts

Glad to see that you could solve problem by your own!

Maik