Sunday 12 September 2021

How to get list of users added to a queue in Apex ?

 There is no separate objects to store queue name and all queue members. They are mapped to Group and Group Member respectively. Queues are stored as records in the table Group with 'Type' as "Queue". 

Select Id from Group where type='Queue' and Name='Queue Name' - This query will return Id of queues.

Use above query results (Id)  in the query [Select UserOrGroupId From GroupMember where GroupId =:grpIdset] to fetch all the users or groups which are members of the required queue. 

We can also write both query combing 

SELECT UserOrGroupId, COUNT( Id ) FROM GroupMember WHERE GroupId IN ( SELECT Id FROM Group WHERE Type = 'Queue' ) GROUP BY UserOrGroupId
 
For User level
SELECT UserOrGroupId, COUNT( Id ) FROM GroupMember WHERE UserOrGroupId =: userfinfo.getUserid() AND GroupId IN ( SELECT Id FROM Group WHERE Type = 'Queue' ) GROUP BY UserOrGroupId 

We know Queue can be created for different objects. We have a section names supported objects, where we can select objects names.

To know which objects this queue contains then we have another standard objec

QueueSobject : - Represents the mapping between a queue Group and the sObject types associated with the queue, including custom objects.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_queuesobject.htm

SELECT QueueId FROM QueueSobject where SobjectType in ('ObjectName','ObjectName2','ObjectName3')






No comments: