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
Abhishek ChopraAbhishek Chopra 

How to Fetch all users of the Queue ?

I have Created a Queue named 'Test Queue 3' where i have added one user(A) and one RoleandSubordinate(B is assigned a role of DirectorDirectSalesD and has one user C as its subordinate), now i need to fetch all the users in this Queue 'Test Queue 3'. 

I went through many posts but did not find any satifactory answers. Any help here is appreciated. Thanks in Advance..!!
Raj VakatiRaj Vakati
You cannt able to get in Single SOQL 

Please refer this link

http://www.johnwestenhaver.com/2015/05/find-all-salesforce-users-in-public.html

 
sowmya Inturi 9sowmya Inturi 9
Hi Abhishek,

As Raj said, you need to use two soql queries to fetch users in the Queue.

Use the following queries:
 [Select Id from Group where type='Queue' and Name='Queue Name'];
[Select UserOrGroupId From GroupMember where GroupId =:IdOfAboveQuery];

Thanks,
Sowmya. 
Abhishek ChopraAbhishek Chopra
Hi Raj and Sowmya,

The issue with the Query provided is you cannot directly extract the users if the Queue contains Roles and Subordinate / Roles and Internal Subordinates.
/* Select UserOrGroupId From GroupMember where GroupId =:IdOfAboveQuery */
This(above) Query will not work if Queue has Roles and Subordinate added as a group in Queue. I think we need to firstly fetch the Role ID from the UserRole Object and then Query the User object with the provided roleId.

/*select Id from UserRole where ParentRoleId IN :roleIds AND ParentRoleID != null */
Please correct me if i'm Wrong.

I got a code from one of the blog posts as under.
*******************************************************************************************************
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(); //005
        String groupType = Schema.SObjectType.Group.getKeyPrefix(); //00G
        Set<Id> groupIdProxys = new Set<Id>();
        // Loop through all group members in a group
        for(GroupMember m : [Select Id, UserOrGroupId, Group.Type, Group.RelatedId 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)
        {    
            Set<id> groupId = new set<id>();
            Set<id> roleId = new set<id>();
            Set<id> roleAndSubId = new set<Id>();

            for(Group g2 : [Select Id, Type, relatedId From Group Where Id = :groupIdProxys]){
                if(g2.Type == 'Role'){  
                    roleId.add(g2.relatedId);    
                }
                else if(g2.Type== 'RoleAndSubordinates'){ 
                    roleAndSubId.add(g2.relatedId);            
                }                                            
                else if(g2.Type== 'PRMOrganization'){        
                    roleId.add(g2.relatedId);                            
                }
                else if(g2.Type== 'Regular'){
                    groupId.add(g2.id);    
                }
                system.debug(g2);
            }
            if(roleAndSubId.size()>0){
                roleId.addAll(getAllSubRoleIds(roleAndSubId));
            }
            if(roleId.size()>0){
                for(User u: [select id from user where UserRoleId IN: roleId]){
                    result.add(u.id);
                }
            }
            if(groupId.size()>0){
                for(GroupMember gm : [select id, UserOrGroupId from GroupMember where GroupId IN: groupId]){
                    result.add(gm.UserOrGroupId);        
                }
            }
        }
        return result;  
    }
    
    public static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {
    
        Set<ID> currentRoleIds = new Set<ID>();
    
        // get all of the roles underneath the passed roles
        for(UserRole userRole :[select Id from UserRole where ParentRoleId IN :roleIds AND ParentRoleID != null limit 40000])
            currentRoleIds.add(userRole.Id);
    
        // go fetch some more rolls!
        if(currentRoleIds.size() > 0)
          currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));
    
        return currentRoleIds;
    
    }

**************************************************************************************************************