- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2018 09:45 PM
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.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2018 02:07 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2018 10:09 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2018 11:06 PM
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()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2018 02:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-21-2021 02:32 AM
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
Scoped
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])