1

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?

1
  • 1
    Just a thought, since you can create groups that do not share w hierarchy, could you essentially clone the group, to a group without hierarchy sharing, and then check the delta w the original group, and these would be the folks who are in as managers?
    – gorav
    Apr 3 '16 at 23:49
2

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

2
  • In salesforce, in the Group details pages under View all users there is a column called Reason For Membership - is there no way to access that field in the API? Apr 4 '16 at 8:21
  • I have edited and added a picture from salesforce showing the information I want to use in my api where clause or restriction code... Apr 4 '16 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.