+ Start a Discussion
andyKal10andyKal10 

API access to queue

I have been given the task of creating a list of our Queues and the members that belong to them. After playing around in the schema browser it seems that this meta-data has enough exposure to be able to generate this list with a soql query. Is there anybody out there who has done this before, and could point me in the right direction.

 

Thanks,

Andy

Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

I was curious so tried to figure it out.  A queue is a type of group, which has a groupMember for each user.  I think this would work, but haven't tested it:

 

String queueName='Marketing Queue'; String queuesObjectType='Lead'; Id queueID = [Select Id from QueueSobject q where q.Queue.Name =:queueName q.SobjectType:=queueSObjectType]; List<id> queueGroupIDs=[Select Id FROM Group WHERE RelatedID IN :queueId]; List<id> queueUserIds=[Select UserOrGroupId FROM GroupMember WHERE GroupId IN :queueGroupIds];

 

 

You can probably nest these, but I broke them out to make it easier to understand.

All Answers

jkucerajkucera

I was curious so tried to figure it out.  A queue is a type of group, which has a groupMember for each user.  I think this would work, but haven't tested it:

 

String queueName='Marketing Queue'; String queuesObjectType='Lead'; Id queueID = [Select Id from QueueSobject q where q.Queue.Name =:queueName q.SobjectType:=queueSObjectType]; List<id> queueGroupIDs=[Select Id FROM Group WHERE RelatedID IN :queueId]; List<id> queueUserIds=[Select UserOrGroupId FROM GroupMember WHERE GroupId IN :queueGroupIds];

 

 

You can probably nest these, but I broke them out to make it easier to understand.

This was selected as the best answer
andyKal10andyKal10

Thanks John. You got me on the right track. The one limitation to your solution is that it is not possible to query further into the GroupOrUserId field which prevents from pulling the user info needed to make the report readable. I tried the following: GroupOrUser.Name, GroupOrUser.User.Name, and maybe some others with no success. So, then I had a look at the schema browser again and under the access info for this field it inidicates that it can't be queried. So, I used the data loader to export a list of my GroupMembers pulling Group.Name and the GroupOrUserId. I then exported all of my users and mapped them together in Access so that  I could pull User Names and other info into my GroupMember table. After a few other maniputlations in Access I got what I needed.

 

Thanks again.

jkucerajkucera

Glad to be of service! If you're interested in automating that last part, you should be able to add another line to access User info programmatically:

 

List queueUsers=[Select Id, FirstName, LastName, Email FROM User WHERE Id IN : queueUserIDs];

 

 

Then you can do things like loop through the users to get the name:

 

for (User u:queueUsers){

  String FirstName=u.FirstName;

}

andyKal10andyKal10

Of Course! Thanks for pointing that out. Makes perfect sense. I wish I thought of it.

 

Rajkumar VenkatRajkumar Venkat

I believe above code doesnot works out for me!!! 

 

I found this one.. 

 

String queueName='level 1 App';
id queueGroupId= [Select Id from Group where type='Queue' and Name= :queueName].Id;
List<sObject> queueUsers=[Select UserOrGroupId FROM GroupMember WHERE GroupId =:queueGroupId];
system.debug(queueUsers);