Skip to main content
added further details for clarification
Source Link

HowEDIT:

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?

How can I restrict or exclude members who are in the group 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?

Source Link

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...

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