function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Our Man In BananasOur Man In Bananas 

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

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

Is it possible to access that information through the api?

If not, how can I restrict or exclude members who are in the group because they are the manager of a member?
Ravi Dutt SharmaRavi Dutt Sharma
I can see that you have already fetched the user id's in a set named "result" from GroupMember object. Now this set "result" also contains the user id's of manager records. So you can issue a query on user object where ManagerId is in result set. Something like below :
 
SELECT Id FROM User WHERE ManagerId IN: result

Store the output in another set named result2. result1 - result2 is what you want. Hope this will solve your issue.
Our Man In BananasOur Man In Bananas
Ravi,

Thanks for your help, but how would I get a set from result2-result?
Ravi Dutt SharmaRavi Dutt Sharma
There is a removeAll method present in Set, you can use that.