[SalesForce] get all members of public group EXCEPT where they are managers if a member

I am using the below code to get the members of a public group, plus all the indirect members (either members of a child group, or associated by their role membership)

public class GetAllGroupMembers {

    public static Set<id> GetUserIdsFromGroup(Id groupId)
    {
    // store the results in a set so we don't get duplicates
    Set<Id> result=new Set<Id>();
    String userType = Schema.SObjectType.User.getKeyPrefix();
    String groupType = Schema.SObjectType.Group.getKeyPrefix();

    // Loop through all group members in a group
    for (GroupMember m : [Select Id, UserOrGroupId From GroupMember Where GroupId = :groupId])
    {
        // If the user or group id is a user
        if (((String)m.UserOrGroupId).startsWith(userType))
        {
            system.debug('its a user:');
            result.add(m.UserOrGroupId);
            }
        // If the user or group id is a group
        // Note: there may be a problem with governor limits if this is called too many times
        else if (((String)m.UserOrGroupId).startsWith(groupType))
        {
            system.debug('its a Group:');
            // Call this function again but pass in the group found within this group
            result.addAll(GetUSerIdsFromGroup(m.UserOrGroupId));
            }
        }   
        return result; 
    }

    public static Set<id> GetUserIdsFromGroupWithProxies(Set<Id> groupIds){

        system.debug('GetUserIdsFromGroupWithProxies: ');            

        // store the results in a set so we don't get duplicates
        Set<Id> result=new Set<Id>();

        String userType = Schema.SObjectType.User.getKeyPrefix();
        String groupType = Schema.SObjectType.Group.getKeyPrefix();

        Set<Id> groupIdProxys = new Set<Id>();

        // Loop through all group members in a group
        for(GroupMember m : [Select Id, UserOrGroupId From GroupMember Where GroupId in :groupIds]){
            // If the user or group id is a user
            if(((String)m.UserOrGroupId).startsWith(userType)){            
                system.debug('Group Member ' + string.valueOf(m.UserOrGroupId) + ' is a user');
                result.add(m.UserOrGroupId);
                }

            // If the user or group id is a group
            // Note: there may be a problem with governor limits if this is called too many times
            else if (((String)m.UserOrGroupId).startsWith(groupType)){

                system.debug('Group Member ' + string.valueOf(m.UserOrGroupId) + ' is a group');
                // Call this function again but pass in the group found within this group
                groupIdProxys.add(m.UserOrGroupId);
                }
            }

        if(groupIdProxys.size() > 0){
            system.debug('adding ' + groupIdProxys.size() + ' proxies');
            result.addAll(GetUserIdsFromGroupWithProxies(groupIdProxys));
            }

        return result;  
    }    
}

The problem I am facing is that we specifically want to exclude those users who are members because they are the manager of a member…

EDIT:

In SalesForce when I look at Setup > Users > Public Groups > Group_Name > View all users I can see this column:

enter image description here
How can I restrict or exclude members who are in the group because they are the manager of a member?

Best Answer

Realistically, no, you can't do this with a simple query, as it also includes the manager's manager, the manager's manager's manager, etc... Basically, you'd need a second query to get everyone's role and the the role's parent role, then iterate over all those values and recursively remove the managers. While you can do this, it is non-trivial, because we are not given the tools to do this directly; the API only has the concept of "role" (which includes managers) and "role and subordinates" (which also includes users below the current role).

Related Topic