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

Chuck Tomasi
Tera Patron

Hi Kiran,



You would need to do a GlideRecord query on the sys_user_grmember table to find the members of a group.



Example (untested): Returns an array of sys_ids for the members of a group.



function getMemberSysIds(groupName) {



        var answer = [];



        var mem = new GlideRecord('sys_user_grmember');


        mem.addQuery('group.name', groupName);


        mem.query();



        while (mem.next()) {


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


        }



        return answer;


}


Hi Chuck Tomasi,


Thanks for the solution.


what will be the value of sys_id ? is it user_name or email_ID or User_ID


I need an array of User_ID.



Regards,


Kiran Pedduri


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());


Hi Chuck Tomasi,



I am using below script, to update watch_list


var watchlist = current.watch_list;


gs.addInfoMessage(watchlist);


current.watch_list =watchlist + "," + addMe;   // This is not working because watchlist is in encoded form where as i am appending user string to watch list.



How do I get current watch_list in user-name string format?



*********************code ***********************


(function executeRule(current, previous /*null when async*/) {


  addToWatchList();


function addToWatchList() {


if(true) {


  var watchlist = current.watch_list;


  gs.addInfoMessage(current.watch_list);


  current.watch_list = getMemberSysIds("P1 P2 Incident Watchers");     //watchlist + "," + addMe;


  current.u_escalation_assigned_to = '';


  //alert(current.watch_list);


}


}


  function getMemberSysIds(groupName) {



        var answer = [];


        var mem = new GlideRecord('sys_user_grmember');


        mem.addQuery('group.name', groupName);


        mem.query();



        while (mem.next()) {


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


        }


// gs.addInfoMessage(answer.toString());


        return answer.toString();


}


})(current, previous);



********************End***************************