[SalesForce] Get ALL queue Users

I am trying to come up with a way to get ALL queue Users (not just the users who have a groupMember record with UserOrGroupId matching theirs) via apex.

Queue membership can also be granted via User, Group, Role, Role and subordinates, Portal User, Portal Role, Portal Role and subordinates, and also if you are above a user in the role hierarchy, you will be granted access.

I cannot be the first person who has solved this problem, and there is a lot to keep track of here.

Thanks!

Best Answer

I think you can combine a couple answers to questions and be able to get something working well.


You listed all the requirements in all the ways someone can be added to a queue. If we simplify it, you have two paths you need to handle based on UserOrGroupId:

  1. Users
  2. Groups (encompasses role, groups, etc)

This is the start of any logic/code you'd have to gather it

SELECT UserOrGroupId from GroupMember where Group.Name = 'insert name here'

You would need to check if the UserOrGroupId is indeed a user or group (getSObjectType())

  • If it's a User, you can add it to a list to save for your final query against User. This question covers this and is the simple part.
  • If it's a group, you have to perform another query against Group
SELECT RelatedId,Type,DeveloperName FROM Group WHERE Id in: groupIds

The Type will be what you need to figure out your next set of logic (I excluded some types, see documentation of all values):

  1. Regular (a regular public group)
  2. Role
  3. RoleAndSubordinates
  4. Organization (all users in org)

For these, you can have different logic to end up with what you want - UserIds.

if it's a regular group

Use the same query as above on Group. Be aware, it's possible to have a group that then has other roles/groups associated with it. You have to follow through the web.

if it's a role

query the active users in that role

SELECT Id FROM User WHERE UserRoleId =: roleId AND isActive = true

if it's a role and subordinate

Query the roles and roles that look up to this role. This question goes over figuring out this problem.

if it's an organization

It's simply all users who are in the queue.


Maps will be your friend here. You'll most likely want to query all Groups, possibly all GroupMembers, and all roles in the organization in 3 queries to start to build maps you can reference throughout.

Related Topic