2

I want get a list of all the users who are part of public group of the currently logged in user.

From what i have read on the documentations, i should be able to pick these values from group and group member objects.

Things begin to get messier while using group member object. I cant really in a sure shot way say a user is part of group

Usage

A record exists for every User or Group who is a direct member of a public group whose Type field is set to Regular. User records that are indirect members of Regular public groups are not listed as group members. A User can be an indirect member of a group if he or she is in a UserRole above the direct group member in the hierarchy, or if he or she is a member of a group that is included as a subgroup in that group.

Can anyone point me towards a best way of getting this list?

5

From my experience, accessing direct group members isn't too bad - you just iterate the group and for all user members just pull their details. If it is a group member, you just recursively call your function that iterates the group.

Where things get really sticky is when you try to figure out the indirect members - I've found that when you start trying to traverse the role hierarchy on a user by user basis, unless you have a flat structure you very quickly run out of SOQL queries.

Here's an example of traversing a group and subgroups to get all user ids:

    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))
            {
                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))
            {
                // Call this function again but pass in the group found within this group
                result.addAll(GetUSerIdsFromGroup(m.UserOrGroupId));
            }
        }

        return result;  
    }
| improve this answer | |
3

I found a bug in your code. Please check it below.

public static Set<id> GetUserIdsFromGroup(Set<Id> groupIds)
{
    // 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))
        {
            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))
        {
            // Call this function again but pass in the group found within this group
            groupIdProxys.add(m.UserOrGroupId);
        }
    }
    if(groupIdProxys.size() > 0)
    {
        result.addAll(GetUSerIdsFromGroup(groupIdProxys));
    }
    return result;  
}
| improve this answer | |
0

Here is the Code below You have to call GetRoleVsUSerMap Method it will return all Users Id under each Group id existing in your Org.

public class AllPublicGroupQuery {

    public static Set<Id> grpids = new Set<Id>();
    public static Set<Id> Roleids = new Set<Id>();
    public static Map<String,Set<String>> finalmap = new Map<String,Set<String>>();  
    Public static Map<String,Set<String>> mapParentToChild = new Map<String,Set<String>>();
    public static Map<String,Set<String>> grpidTomemberid = new Map<String,Set<String>>();
    public static Map<String,Set<String>> MapgrpToRole = new Map<String,Set<String>>();
    public static Map<String,Set<String>> MapgrpToRoleSub = new Map<String,Set<String>>();
    public static Map<String,Set<String>> MapgrpToUser = new Map<String,Set<String>>();

    public static Map<String,Set<String>> GetRoleVsUSerMap(){
        Map<String,String> mapGrpRelatedids = new Map<String,String>();
        Map<Id,string> mapgrpType = new Map<Id,String>();
        Set<Id> userOrGrpids = new Set<Id>();
        Set<String> grpparentids = new Set<String>();

        for(GroupMember gpmem : [SELECT GroupId,Id,UserOrGroupId FROM GroupMember]){
            if(gpmem.UserOrGroupId != null)
                userOrGrpids.add(gpmem.UserOrGroupId);
        }
        for(Group gp:[SELECT Id,Type,RelatedId FROM Group where Id In:userOrGrpids ]){
            grpids.add(gp.Id);
            if(gp.Type != NULL)
                mapgrpType.put(gp.Id,gp.type);
            if(gp.RelatedId != NULL && (gp.Type == 'Role' || gp.Type == 'RoleAndSubordinates'))
                mapGrpRelatedids.put(gp.Id, gp.RelatedId);
        }

        for(GroupMember grpmem : [SELECT GroupId,UserOrGroupId FROM GroupMember ORDER BY GroupId DESC NULLS LAST]){

            if(grpidTomemberid.get(grpmem.GroupId) == NULL){
                Set<String> ids = new Set<String>();
                ids.add(grpmem.UserOrGroupId);
                grpidTomemberid.put(grpmem.GroupId,ids);
            }
            else{
                grpidTomemberid.get(grpmem.GroupId).add(grpmem.UserOrGroupId);
            }
        }

        for(String mapkey : grpidTomemberid.keySet()){
            for(String keyval : grpidTomemberid.get(mapkey)){
                if(mapgrpType.get(keyval) == 'Role'){
                    if(MapgrpToRole.get(mapkey) == NULL){
                        Set<String> ids = new Set<String>();
                        ids.add(mapGrpRelatedids.get(keyval));
                        MapgrpToRole.put(mapkey,ids);
                    }
                    else{
                        MapgrpToRole.get(mapkey).add(mapGrpRelatedids.get(keyval));  
                    }
                }
                else if(mapgrpType.get(keyval) == 'RoleAndSubordinates'){
                    if(MapgrpToRoleSub.get(mapkey) == NULL){
                        Set<String> ids = new Set<String>();
                        ids.add(mapGrpRelatedids.get(keyval));
                        MapgrpToRoleSub.put(mapkey,ids);
                    }
                    else{
                        MapgrpToRoleSub.get(mapkey).add(mapGrpRelatedids.get(keyval));  
                    }
                }
                else if(mapgrpType.get(keyval) == 'Regular'){
                    if(mapParentToChild.get(mapkey) == NULL){
                        Set<String> ids = new Set<String>();
                        ids.add(keyval); 
                        mapParentToChild.put(mapkey,ids);
                    }
                    else{
                       mapParentToChild.get(mapkey).add(keyval); 
                    }
                }
                else{
                    if(MapgrpToUser.get(mapkey) == NULL){
                        Set<String> ids = new Set<String>();
                        ids.add(keyval); 
                        MapgrpToUser.put(mapkey,ids);
                    }
                    else{
                       MapgrpToUser.get(mapkey).add(keyval); 
                    }
                }
            }
        }

        /***************Here we iterate Group under group ***********************/

        if(mapParentToChild != NULL && mapParentToChild.keySet().size()>0 ){
            for(String parentid : mapParentToChild.keySet()){
                for(String childid : mapParentToChild.get(parentid)){
                     if( MapgrpToRoleSub.get(childid).size()>0 && MapgrpToRole.get(childid) !=NULL){
                         if(MapgrpToRole.get(parentid) == NULL){
                            Set<String> ids = new Set<String>();
                            ids.addAll(MapgrpToRole.get(childid));
                            MapgrpToRole.put(parentid,ids);
                         }
                         else
                            MapgrpToRole.get(parentid).addAll(MapgrpToRole.get(childid));
                     }
                     if(MapgrpToRoleSub.get(childid).size()>0 && MapgrpToRoleSub.get(childid) !=NULL){
                         if(MapgrpToRoleSub.get(parentid) == NULL){
                            Set<String> ids = new Set<String>();
                            ids.addAll(MapgrpToRoleSub.get(childid));
                            MapgrpToRoleSub.put(parentid,ids);
                         }
                         else
                            MapgrpToRoleSub.get(parentid).addAll(MapgrpToRoleSub.get(childid));
                     }
                     if(MapgrpToUser.get(childid).size()>0 && MapgrpToUser.get(childid) != NULL){
                        if(MapgrpToUser.get(parentid) == NULL){
                            Set<String> ids = new Set<String>();
                            ids.addAll(MapgrpToUser.get(childid));
                            MapgrpToUser.put(parentid,ids);
                        }
                        else
                            MapgrpToUser.get(parentid).addAll(MapgrpToUser.get(childid));
                     }  
                }
            }
            queryGroupuser(MapgrpToRole,MapgrpToRoleSub,MapgrpToUser);
        }
        system.debug('----finalmap---->'+finalmap);
        return finalmap;
    }
    public Static void queryGroupuser(Map<String,Set<String>> rolemap,Map<String,Set<String>> rolesubmap,Map<String,Set<String>> usrmap){
        Map<String,List<String>> maproleAndUsr = new Map<String,List<String>>();
        Map<Id,List<Id>> copymaproleAndUsr = new Map<Id,List<Id>>();
        Map<String,Set<String>> mapSubandUsr = new Map<String,Set<String>>();

        if(rolemap.keySet().size() > 0){
            for(User usr: [SELECT UserRoleId,Id FROM User where Name != 'Chatter Expert']){
                if(maproleAndUsr.get(usr.UserRoleId) == null && usr.UserRoleId !=Null){
                   List<String> ids= new List<String>();
                   ids.add(usr.Id);
                   maproleAndUsr.put(usr.UserRoleId,ids);
                }else if(usr.UserRoleId !=Null)
                    maproleAndUsr.get(usr.UserRoleId).add(usr.Id);
            }
            for(String rolekey : rolemap.keySet()){
                for(String rolevalue : rolemap.get(rolekey)){
                    if(finalmap.get(rolekey) == NULL){
                        Set<String> ids = new Set<String>();
                        if(maproleAndUsr.get(rolevalue) != NULL && maproleAndUsr.get(rolevalue).size() > 0)
                            ids.addAll(maproleAndUsr.get(rolevalue));
                        finalmap.put(rolekey,ids);
                    } 
                    else if(maproleAndUsr.get(rolevalue) != NULL && maproleAndUsr.get(rolevalue).size() > 0)
                        finalmap.get(rolekey).addAll(maproleAndUsr.get(rolevalue));
                }
            }
        }
        if(rolesubmap.keySet().size() > 0){
            for(UserRole usr: [SELECT Id FROM UserRole order by Name]){
                RoleIds.add(Usr.Id);
            }
            copymaproleAndUsr =  AllUserRoleQuery.GetRoleVsUSerMap(RoleIds);
            for(Id key : copymaproleAndUsr.keySet()){
                Set<String> ids = new Set<String>();
                for(Id value : copymaproleAndUsr.get(key)){  
                        ids.add((String)value);
                }
                mapSubandUsr.put((String)key,ids);

            }


            for(String subrolekey : rolesubmap.keySet()){
                for(String subrolevalue : rolesubmap.get(subrolekey)){
                    if(finalmap.get(subrolekey) == NULL){
                        Set<String> ids = new Set<String>();
                        if(mapSubandUsr.get(subrolevalue) != NULL)
                            ids.addAll(mapSubandUsr.get(subrolevalue));
                        finalmap.put(subrolekey,ids);
                    } 
                    else if(mapSubandUsr.get(subrolevalue) != NULL)
                        finalmap.get(subrolekey).addAll(mapSubandUsr.get(subrolevalue));
                }
            }
        }

        if(usrmap.keySet().size() > 0){
            for(String usrkey : usrmap.keySet()){
                   if(finalmap.get(usrkey) == NULL){
                        Set<String> ids = new Set<String>();
                        if(usrmap.get(usrkey) != NULL)
                            ids.addAll(usrmap.get(usrkey));
                        finalmap.put(usrkey,ids);
                   }
                   else if(usrmap.get(usrkey) != NULL)
                       finalmap.get(usrkey).addAll(usrmap.get(usrkey));
            }
        }
    }
}
| improve this answer | |

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.