Is there a declarative way to get all of the groups a user leads or manages?

Craig Jacobs
Giga Contributor

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.

5 REPLIES 5

SumanthDosapati
Mega Sage
Mega Sage

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.

ideamax
Mega Expert

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.

 

Craig Jacobs
Giga Contributor

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.

Hitoshi Ozawa
Giga Sage
Giga Sage

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