Get the Users of specific Group

Rakshanda Kunte
Tera Contributor

Hi All,

 

I have 2 field Members (List Collector) and Group Name (Reference) in a catalog item.

 

Whenever a requester selects a group so in the Member field the list of users in who is part of that group should populate as options to select. 

 

RakshandaKunte_0-1698839403487.png

 

 

Kindly, help with the scripting.

 

Thank you..!!

1 ACCEPTED SOLUTION

@Rakshanda Kunte 

something like this in advanced ref qualifier

javascript: var query;
var arr = [];
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("group", current.variables.group_name);
gr.query();
while (gr.next()) {
   arr.push(gr.getValue('user'));
}
query = 'sys_idIN' + arr.toString();
query;

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

View solution in original post

7 REPLIES 7

Anand Kumar P
Giga Patron
Giga Patron

Hi @Rakshanda Kunte ,

On Members field write reference qualifier and use below script

javascript:"sys_idIN" + new scriptIncludeName().scriptIncludeFunctionName(current.variables.group_name);
where "current.variables.group_name" is the way to call the variable that contains the group (update the name "group_name" according to the right name of your variable).

Create new script include with name-

getMembersOfGroup: function(group_name) {
    var result = [];
    var gr = new GlideRecord("sys_user_grmember");
    gr.addQuery("group", group_name);
    gr.query();
    while (gr.next()) {
        result.push(gr.getValue("user"));
    }

    return result.join(",");

}

Please mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Rakshanda Kunte 

it should show only those users who belong to the selected group?

if yes then you should use advanced ref qualifier

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Rakshanda Kunte 

something like this in advanced ref qualifier

javascript: var query;
var arr = [];
var gr = new GlideRecord("sys_user_grmember");
gr.addQuery("group", current.variables.group_name);
gr.query();
while (gr.next()) {
   arr.push(gr.getValue('user'));
}
query = 'sys_idIN' + arr.toString();
query;

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

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

Vishal Birajdar
Giga Sage

Hi @Rakshanda Kunte 

 

You can write script include : 

Name : GetGroupMemberUtil

Function Name : groupMembers()

var GetGroupMemberUtil = Class.create();
GetGroupMemberUtil.prototype = {
    initialize: function() {},

    groupMemebers: function(groupSysId) {

        var array = [];
        var grMem = new GlideRecord('sys_user_grmember');
		grMem.addEncodedQuery("group=" + groupSysId);
       // grMem.addQuery('group', groupSysId);
        grMem.query();
        while (grMem.next()) {
            array.push(grMem.user);
        }
        return "sys_idIN" + array.toString();

    },
    type: 'GetGroupMemberUtil'
};

 

& write reference qualifier on List collector field -

 

VishalBirajdar_0-1698840491530.png

 

javascript&colon; var query = new GetGroupMemberUtil().groupMemebers(current.variables.<your_variable_name>);
query;

 

Hope this helps...!!

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates