Steven I know is late but hopefully, this helps someone, I had a similar request, to check where the user is in a queue, that can have inside groups. roles, and roles with subordinates, for this I made some recursive methods, to check each of the members of the queue, it will be better to use providers to avoid extending the logic
private User oldUser;
private Map<id, Group> groupMap;
private Map<id, User> UserMap;
private Map<id,List<GroupMember>> groupMembers;
private Map<id,List<UserRole>> roleHierarchy;
public Boolean isUserOnQueue(Id queueId, Id UserId){
this.oldUser = this.getUser(oldOwnerId);
if(this.oldUser == null) {
return false;
}
return isMember(newOwnerId);
}
private Boolean isMember(Id groupId) {
Group groupRecord = this.getGroupObject(groupId);
if(groupRecord == null) {
return false;
}
switch on groupRecord.Type {
when 'Regular', 'Queue' {
return isMemberFromGroup(groupId);
} when 'RoleAndSubordinatesInternal' {
return isMemberFromRole(groupRecord.RelatedId, true);
} when 'Role' {
return isMemberFromRole(groupRecord.RelatedId, false);
}
}
return false;
}
private Boolean isMemberFromGroup(Id groupId) {
for(GroupMember member : this.getGroupMembers(groupId)) {
if(member.UserOrGroupId.getSobjectType() == Group.getSObjectType()) {
if(this.isMember(member.UserOrGroupId) == true) {
return true;
}
} else if(oldUser.Id == member.UserOrGroupId) {
return true;
}
}
return false;
}
private Boolean isMemberFromRole(Id roleId, Boolean withSubordinates) {
if(oldUser.UserRoleID == roleId) {
return true;
}
if(withSubordinates) {
for(UserRole role : this.getRoleHierarchy(roleId)) {
if(this.isMemberFromRole(role.Id, withSubordinates) == true) {
return true;
}
}
}
return false;
}
private User getUser(ID userId) {
if(this.UserMap == null) {
this.UserMap = new Map<Id, User>([select id,UserRoleID from User]);
}
return this.UserMap.get(userId);
}
private Group getGroupObject(Id groupId) {
if(this.groupMap == null) {
this.groupMap = new Map<Id, User>([select id,Type,RelatedId from Group]);
}
return this.groupMap.get(userId);
}
private List<GroupMember> getGroupMembers(Id groupId) {
if(this.groupMembers == null) {
List<groupMember> tempList = [select id, userOrGroupId, GroupId, Group.Name, Group.DeveloperName, Group.Type from groupMember]);
for(groupMember member:tempList) {
List<groupMember> currentMembers = this.groupMembers.get(groupMember.GroupId) ?? new List<groupMember>();
this.currentMembers.add(member);
this.groupMembers.put(groupMember.GroupId, this.currentMembers)
}
}
return this.groupMembers.get(userId);
}
private List<UserRole> getRoleHierarchy(Id roleId) {
if(this.roleHierarchy == null) {
Map roleMap = new Map<Id, Role>([SELECT Id, Name, DeveloperName, ParentRoleId FROM UserRole]);
for (UserRole role : roleMap.values()) {
if (!this.roleHierarchy.containsKey(role.Id)) {
this.roleHierarchy.put(role.Id, new List<UserRole>());
}
if (role.ParentRoleId == null) { continue; }
if (!this.roleHierarchy.containsKey(role.ParentRoleId)) {
this.roleHierarchy.put(role.ParentRoleId, new List<UserRole>());
}
this.roleHierarchy.get(role.ParentRoleId).add(role);
}
}
return this.roleHierarchy.get(roleId)
}