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

Maik Skoddow
Tera Patron
Tera Patron

Hi @Kub 

in the form view the JavaScript object g_list is not available. Replace it with g_form.

Kind regards
Maik

Thank you for a reply, how do I access the list type element to modify its choices?

Best regards,
Kub

Ankur Bawiskar
Tera Patron
Tera Patron

@Kub 

Hi,

if you wish to add or remove option you can use g_form

Removing or Disabling Choice List Options

your script would be like this

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

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

	//Remove the unnecessary choices

	if(role == 'architect'){
		g_form.removeOption('responsibility_template','low');
	}
	if(role == 'developer'){
		g_form.removeOption('responsibility_template','high');
	}

}

I would suggest this in your client script

1) onchange happens clear the choice from responsibility_template field

2) then based on role add the relevant options

Regards
Ankur

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

Thank you for the response,

This solution doesn't work for list type field. It works perfectly fine for choice type fields only. 

Best regards,
Kub