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:
This is the start of any logic/code you'd have to gather it
You would need to check if the UserOrGroupId is indeed a user or group (getSObjectType())
User
. This question covers this and is the simple part.The Type will be what you need to figure out your next set of logic (I excluded some types, see documentation of all values):
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
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.