- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 11:44 PM
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
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?
Solved! Go to Solution.
- Labels:
-
Scoped App Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 01:46 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2022 11:59 PM
Hi
in the form view the JavaScript object g_list is not available. Replace it with g_form.
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 12:04 AM
Thank you for a reply, how do I access the list type element to modify its choices?
Best regards,
Kub
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 12:44 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2022 12:59 AM
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