Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get all users from group

kiranped
Giga Contributor

Hi All,

Is there any js-function to get the user list object by group-name or is there any script to get all users from a group?

example group name is: IS&T Senior Leadership Team

Thanks & Regards,

Kiran Pedduri

1 ACCEPTED SOLUTION

Hi Kiran,



Sorry, I had an error in the first one. I was pushing the sys_id of the membership record, not the users. I've corrected it.



Easy change... You can 'dot-walk' to get that. If you want the user_name field (labeled User ID, like chuck.tomasi) you can change the answer.push() line to this



answer.push(mem.user.name.toString()).



You want the email,answer.push(mem.user.email.toString());


View solution in original post

22 REPLIES 22

Make two changes. First, don't get the user's name. List fields (like the watch list) are comma separated sys_ids.   Change your answer.push() to this:



answer.push(mem.getValue('user'));



and return the comma separated list this way:



return answer.join(',');


Hi Tomasi,


I am trying the below script please check i am not getting users in the group


function getMemberSysIds(groupName) {


var answer = [];


  var mem = new GlideRecord('sys_user_grmember');


        mem.addQuery('group.name', IS-HOSTING-SSOIN-WINTEL-DC-Global);


        mem.query();


        while (mem.next()) {


answer.push(mem.user.email.toString());


gs.print( test);


        }


        return answer;


}


gs.print(test);


gs.print('test');


gs.print(answer);


Please have the group name in quotes.



mem.addQuery('group.name', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


HI Shishir,


I have given code as below, but i am getting error in back ground script, pl ind the below screen shot


function getMemberSysIds(groupName) {


var answer = [];


  var mem = new GlideRecord('sys_user_grmember');


        mem.addQuery('group.name', 'IS-HOSTING-SSOIN-WINTEL-DC-Global');


        mem.query();


        while (mem.next()) {


var test =answer.push(mem.user.email.toString());


gs.print( test);


        }


        //return answer;


}


gs.print(test);


gs.print('test');


find_real_file.png


you have to put test in quotes if test is not defined as variable. remove the gs.print(test) and just keep gs.print('test');