getMyGroups() not working properly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi all,
I am using getMyGroups() in my code to get all groups. It is returning one extra group. Why?
I know below thing can be achieved by other ways, but I want to use getMyGroups().
My code-
*** Script: Name HR Tier 1
*** Script: Name App Engine Admins
*** Script: Name Conditional Script Writer
*** Script: Name Team Development Code Reviewers
*** Script: Name HR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
All these groups don't have Parent except HR Tier 1 , it is a child of HR group.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
getMyGroups() returns parent group as well for the child to which you belong
So you should query sys_user_grmember to get exact group to which you belong
var groups = [];
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', 'IN', gs.getUserID());
gr.query();
while (gr.next()) {
groups.push(gr.group.name.toString());
}
gs.info('groups are' + groups.toString());
💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader