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

SanjivMeher
Kilo Patron
Kilo Patron

It says it returns the list of sysids of the groups.

 

You can write a script to get the list of group names.

var grpList = '';

var grm = new GlideRecord('sys_user_grmember');

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

grm.query();

while (grm.next())

{

if (grpList)

grpList = grpList+','+grm.group.getDisplayValue();

else

grpList = grm.group.getDisplayValue();

}

gs.addInfoMessage(grpList);


Please mark this response as correct or helpful if it assisted you with your question.

DS20
Tera Contributor

I have tried this it's just giving me one group name not all the list which user belongs. 

how to get by using getMyGroups.toArray()

 

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

 

One observation this approach works, but doesn't return parent groups (parents of groups the user is a member of) which "getMyGroups()" does - which may or may not be important, depending on the scenario.

In terms of the original question (running in a Scoped Application) I believe the problem is that the return type from "getMyGroups()" is different in Global and Scoped running. See the following via Xplore.

var groups = gs.getUser().getMyGroups();
groups

Global

find_real_file.png

Scoped

find_real_file.png

i.e. a different Object type is returned in either case.

I found the following (using ArrayUtil) works in both Scoped and Global (I understand the Original was 2 yrs ago, so things may have changed). 

var groups = gs.getUser().getMyGroups();

var arrayUtil = new global.ArrayUtil();

var grpArray = arrayUtil.convertArray(groups);

gs.print(grpArray[0])

See this ArrayUtil doc