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.

How to get group names using getMyGroups() method.

DS20
Tera Contributor

Hello,

   I wanted to get the group names of the user. I have tried using  the getMyGroups() method, but its giving sys_id of the group not the display value of the group name.

Please help me in this.

 

Thanks in advanced.

find_real_file.png

1 ACCEPTED SOLUTION

If you want it in an array you can just alter Sanjivs example a bit. If you run the code below as a fix script you should first get the names of all groups you are apart of followed by a line with just the first group in that array

 

var grpArray = [];
var grm = new GlideRecord('sys_user_grmember');

grm.addQuery('user',gs.getUserID());

grm.query();
while (grm.next())
{
	grpArray.push(grm.group.getDisplayValue());
}

gs.print(grpArray);
gs.print(grpArray[0]);

Now if you want to use this like you do with getMyGroups() then you have to create a script include which you can call upon just like you do with getMyGroups.

https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/concept/c_ScriptIncludes.html

 

View solution in original post

8 REPLIES 8

TD14
Kilo Contributor
var sys_ids = gs.getUser().getMyGroups().toArray();
names = getNames(sys_ids);
gs.info(names.join(', '));



function getNames(sys_ids ){
var names = [];
for (i=0;i<sys_ids .length;i++){
var gr = new GlideRecord('sys_user_group');
gr.get(sys_ids[i]);
names.push(gr.name.toString());
}
return names;
}

varshap04051753
Tera Contributor
var groups = gs.getUser().getMyGroups();
gs.print(groups);

var arrayUtil = new global.ArrayUtil();
var grpArray = arrayUtil.convertArray(groups);

var gr = new GlideRecord('sys_user_group');
gr.get(grpArray[0]);
gs.print(gr.name);

varshap04051753
Tera Contributor
var test = gs.getUser().getMyGroups().toArray();
 var gr = new GlideRecord('sys_user_group');
gr.get(test[0]);
 gs.print(gr.name);

Ramjee
Tera Expert

Hi @DS20 ,

You can try with this script, it will work

var grpArray = [];
var grm = new GlideRecord('sys_user_grmember');
grm.addQuery('user',gs.getUserID());
grm.query();
while (grm.next())
{
    grpArray.push(grm.group.getDisplayValue());
}
gs.print(grpArray);