- 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
02-01-2022 01:22 AM
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
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 01:38 AM
Using DOM is not recommended
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 01:39 AM
Thank you for marking my response as helpful.
if that helped please mark it correct and close the thread.
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 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
02-01-2022 02:00 AM
Glad to see that you could solve problem by your own!
Maik