Get all users, who are part of the requester's groups

Harsha Pappala
Kilo Sage

Hello guys,

For a catalog request, i want to update all the incidents. Where assigned or caller is a user who is part of requester's group/groups. 

Using below script to get the information. But it is very time taking, as i have to parse multiple incidents.

Could anybody suggest me a better way to do this

var sg = new GlideRecord('sys_user_grmember');
sg.addQuery('user', requester);
sg.query();
var group_list = [];
var num = sg.getRowCount();
while (sg.next()) {
gs.info('groups found' + num);
group_list.push(sg.group);
}

for (var j = 0; j < group_list.length; j++) {

var users = [];
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group', group_list[j]);
grp.query();

while (grp.next()) {
users.push(grp.user);
gs.info('user name ' + users[j].getDisplayValue());
}
}

1 ACCEPTED SOLUTION

Willem
Giga Sage
Giga Sage

You got above code working right? So "requester" variable is already set somewhere in the code?

Then this will improve the speed. It will only trigger 2 queries instead of 1 query, plus additional queries for each group:

var sg = new GlideRecord('sys_user_grmember');
sg.addQuery('user', requester);
sg.query();
var group_list = [];
var num = sg.getRowCount();
while (sg.next()) {
    gs.info('groups found' + num);
    group_list.push(sg.group);
}


var users = [];
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('group', 'IN', group_list);
grp.query();
while (grp.next()) {
    users.push(grp.getUniqueValue());
    gs.info('user name ' + grp.getDisplayValue());
}

//now you have an array of users you can use for an "IN" on the incident table

View solution in original post

14 REPLIES 14

I tried this approach, but it did not work.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Harsha,

Can you explain what are you trying to perform?

Regards
Ankur

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

Hi, 

I need to get all the users, who are part of the same groups as the requester of the catalog item . 

In other words, i need to find all my team mates, even if i am part of different teams

I need to compare them to assigned or caller of the incidents..

@Harsha Pappala 

So is that requester a variable on your catalog item or you are saying the user who requested for the catalog item?

I assume your script is in workflow run script of Catalog Item or BR on sc_req_item

sg.addQuery('user', current.variables.<requesterVariable>); // give proper variable name here

if you are saying requested_for field of sc_request then it should be like this

sg.addQuery('user', current.request.requested_for);

Regards
Ankur

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

I need to query all users who are part of the requester's group

For example

Archie is part of HR and MR groups. 

If archie is the requester, my query will be like 

sg.addQuery('user', users of both HR and MR groups);