- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 02:43 PM
Okay so I am trying to write a script include to use as a reference qualifier. The idea is that when assigning an incident to a group, only groups that have the ITIL role should be included in the list to choose from.
I found a script here that does pretty much exactly as I want with the assigned_to field. But I'm really really struggling with how to modify it for the assignment group.
Here's what I have so far:
function u_getGroupsInRole(roleList) { //declare function to be called and expected variable
var roleListIds;
if (roleList) {
roleListIds = getRoleListIds(roleList); //use the function defined below to get the sysIDs of the role entered into the variable
}
var groups = {};
var gr = new GlideRecord('sys_group_has_role');
if (roleListIds) {
gr.addQuery('role', roleListIds);
}
gr.query();
while (gr.next()) {
groups[gr.group.toString()] = true;
}
var ids = [];
for (var id in groups)
ids.push(id);
return ids;
}
// get sys_id's for the named roles
function getRoleListIds(roleList) {
var roleIds = [];
var grRole = new GlideRecord('sys_user_role');
grRole.addQuery('name','IN',roleList);
grRole.query();
while (grRole.next()) {
ids.push(grRole.sys_id.toString());
}
return roleIds;
}
And in the reference qualifier field for assignment_group i have the following:
javascript: u_getGroupsInRole('itil')
However, when I click the search button on the assignment group I'm still getting every group in my instance.
What am I doing wrong?
Bonus question:
How is "while (gr.next()) {groups[gr.group.toString()] = true;" supposed to populate groups with an array of stings? Shouldn't it be something like "while (gr.next()){ groups=gr.group.toString() }; " Why is "= true;" at the end? Shouldn't it be something like "while (gr.next()) = true {" ?
Extra bonus question:
I'm new to javascript (obviously) but how am I able to call a function that further down in the code? shouldn't I have to call a function after the function has actually been declared?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 03:10 PM
Try this:
- function u_getGroupsInRole(roleList) { //declare function to be called and expected variable
- var groups = [];
- var gr = new GlideRecord('sys_group_has_role');
- if (roleList) {
- gr.addQuery('role.name', 'IN', roleList);
- }
- gr.query();
- while (gr.next()) {
- groups.push(gr.group.toString());
- }
- var str = "";
- if(groups.length>-1){
- str = "sys_idIN"+groups.toString();
- }
- return str;
- }
Your reference qualifier call will be
javascript: u_getGroupsInRole('itil,admin');

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 02:54 PM
Use this in the script include
function u_getGroupsInRole(roleList) { //declare function to be called and expected variable
var arr=[];
var gr= new GlideRecord('sys_group_has_role');
gr.addQuery('role.name',roleList);
gr.query();
while(gr.next()){
arr.push(gr.getValue('group'));
}
return 'sys_idIN'+arr.join();
}
now in ref qualifier use this
javascript: u_getGroupsInRole('itil');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 04:31 PM
I found this to work when passed one role, but a list of roles caused the reference qualifier to return nothing.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 03:10 PM
Try this:
- function u_getGroupsInRole(roleList) { //declare function to be called and expected variable
- var groups = [];
- var gr = new GlideRecord('sys_group_has_role');
- if (roleList) {
- gr.addQuery('role.name', 'IN', roleList);
- }
- gr.query();
- while (gr.next()) {
- groups.push(gr.group.toString());
- }
- var str = "";
- if(groups.length>-1){
- str = "sys_idIN"+groups.toString();
- }
- return str;
- }
Your reference qualifier call will be
javascript: u_getGroupsInRole('itil,admin');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 04:33 PM
This actually worked perfectly. Makes me wonder why the script in the reference qualifiers documentation is so complicated.
I'm actually able to understand how this script works except for the "sys_idIN" part.