Is there a declarative way to get all of the groups a user leads or manages?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 08:07 AM
Hello Community. I need to find all of the groups that a user manages or leads, including groups of which the user is not a member. I guess it's common for a user to manage a group, but not be a member of the group. An imperative implementation could just iterate over all groups and test if the user is manager or leader, but that seems really inefficient. I'm looking for something declarative like gs.getUser().getMyGroups() which includes groups the user leads or manages but for which the user is not a member. If I have to go imperative, what's the best way to narrow down the number of records I need to check?
Thank you in advance, this community has always been super helpful.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 08:10 AM
To reduce the number of groups you search, you can add a filter where lead field is empty. However all other group records needs to be checked if you want to get the list.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2022 08:17 AM
You can create a simple functions in a script include
1. allManagedGroups(userID); - all groups managed by userID
2. allActiveManagedGroups(userID) - Only fetch active groups
3. allActiveGroupsWithMembers(userID)
You can create different combinations of function as per your need.
Once created you use these scripts wherever you need.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2022 03:39 PM
Thank you both. I ended up using a query to filter the records, but then had to iterate over all of the records the query returned. It seems performant enough, but doesn't benefit directly from improvements in the platform like a Declarative expression would.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2022 09:48 PM
Hi Craig,
Something like the following Script Include.
var UserUtils = Class.create();
UserUtils.prototype = {
initialize: function() {},
getManagedGroups: function(userSysId) {
var groupList = [];
var gr = new GlideRecord('sys_user_group');
gr.addQuery('manager', userSysId);
gr.query();
while (gr.next()) {
groupList.push(gr.getValue('name').toString());
}
return groupList.join(',');
},
type: 'UserUtils'
};
Execution sample using Scripts - Background
var userUtils = new UserUtils();
var groupList = userUtils.getManagedGroups('62826bf03710200044e0bfc8bcbe5df1');
gs.info(groupList);
Result:
*** Script: Problem Analyzers,Problem Solving