- 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 04:39 PM
LOL you did not mention that you are passing multiple roles. Hard work was mine and credit goes to someone else
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2016 05:28 PM
Hey sorry, didn't mean to offend.
I'm actually comparing the two to determine why one works with multiple roles and the other doesn't, so your efforts have actually been really helpful. I would have marked both correct but the forum doesn't allow for that.
The best that I can tell is the gr.addQuery line. His has x, IN, y where yours is just (x, y).
As it turns out
var u_getGroupsInRole = Class.create();
u_getGroupsInRole.prototype = {
initialize: function() {
},
create: function(roleList) {
var arr=[];
var gr= new GlideRecord('sys_group_has_role');
gr.addQuery('role.name', 'IN' ,roleList);
gr.query();
while(gr.next()){
arr.push(gr.getValue('group'));
}
return 'sys_idIN'+arr.join();
},
type: 'u_getGroupsInRole'
};
actually works too when I use javascript: new u_getGroupsInRole().create('itil,admin');
