Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a helper class to solve this exact problem.</p> <pre><code>public with sharing class GroupsHelper { private static final String userPrefix = User.SObjectType.getDescribe().getKeyPrefix(); public Map&lt;Id, Set&lt;Id&gt;&gt; roleUsers { get; private set; } public Map&lt;Id, Set&lt;Id&gt;&gt; roleSubordinateUsers { get; private set; } public Map&lt;Id, Set&lt;Id&gt;&gt; groupsUsers { get; private set; } public Map&lt;Id, Set&lt;Id&gt;&gt; usersGroups { get; private set; } public Map&lt;Id, Set&lt;Id&gt;&gt; usersQueues { get; private set; } public GroupsHelper() { this.roleUsers = new Map&lt;Id, Set&lt;Id&gt;&gt;(); this.roleSubordinateUsers = new Map&lt;Id, Set&lt;Id&gt;&gt;(); this.groupsUsers = new Map&lt;Id, Set&lt;Id&gt;&gt;(); this.usersGroups = new Map&lt;Id, Set&lt;Id&gt;&gt;(); this.usersQueues = new Map&lt;Id, Set&lt;Id&gt;&gt;(); refresh(true, true); } public void refresh(Boolean refreshRoles, Boolean refreshGroups) { if(refreshRoles) { refreshRoles(); } if(refreshGroups) { refreshGroups(); } } public void refreshRoles() { roleUsers.clear(); roleSubordinateUsers.clear(); //Populate a Map with the role Id and all the users Map&lt;Id, UserRole&gt; roles = new Map&lt;Id, UserRole&gt;([ SELECT Id, Name, ParentRoleId, ( SELECT Id, Name FROM Users ) FROM UserRole]); Set&lt;Id&gt; userIds; Id subordinateId; for(UserRole role : roles.values()) { //Add the User Ids of the current role roleUsers.put(role.Id, (new Map&lt;Id, User&gt;(role.Users)).keySet()); //Loop throuh and populate the role and subordinate Map subordinateId = null; while(role != null) { if(!roleSubordinateUsers.containsKey(role.Id)) { roleSubordinateUsers.put(role.Id, new Set&lt;Id&gt;()); } userIds = roleSubordinateUsers.get(role.Id); if(roleSubordinateUsers.containsKey(subordinateId)) { userIds.addAll(roleSubordinateUsers.get(subordinateId)); } for(User user : role.Users) { userIds.add(user.Id); } subordinateId = role.Id; role = roles.get(role.ParentRoleId); } } } public void refreshGroups() { groupsUsers.clear(); usersGroups.clear(); Map&lt;Id, Group&gt; groups = new Map&lt;Id, Group&gt;([ SELECT Id, Name, Type, RelatedId, ( SELECT UserOrGroupId FROM GroupMembers ), ( SELECT Id, SobjectType FROM QueueSobjects ) FROM Group]); List&lt;Id&gt; stack; Set&lt;Id&gt; userIds, queueIds; Group groop; for(Id groupId : groups.keySet()) { stack = new List&lt;Id&gt; { groupId }; userIds = new Set&lt;Id&gt;(); do { groop = groups.get(stack.remove(0)); //This is extra cautious, it should not be possible to get a null value if(groop != null) { if(groop.RelatedId != null) { if(groop.Type == 'Role') { userIds.addAll(roleUsers.get(groop.RelatedId)); } else if(groop.Type == 'RoleAndSubordinates') { userIds.addAll(roleSubordinateUsers.get(groop.RelatedId)); } } else { for(GroupMember member : groop.GroupMembers) { if(isPrefix(member.UserOrGroupId, userPrefix)) { userIds.add(member.UserOrGroupId); } else { //When the Id is not a user Id it is reliably a group Id, that we can add to the stack for processing stack.add(member.UserOrGroupId); } } } } } while(!stack.isEmpty()); if(userIds.size() &gt; 0) { groupsUsers.put(groupId, userIds); for(Id userId : userIds) { if(!usersGroups.containsKey(userId)) { usersGroups.put(userId, new Set&lt;Id&gt;()); } usersGroups.get(userId).add(groupId); } } } for(Id userId : usersGroups.keySet()) { if(!usersQueues.containsKey(userId)) { usersQueues.put(userId, new Set&lt;Id&gt;()); } for(Id groupId : usersGroups.get(userId)) { groop = groups.get(groupId); if(groop.Type == 'Queue' &amp;&amp; groop.QueueSobjects.size() &gt; 0) { usersQueues.get(userId).addAll((new Map&lt;Id, QueueSobject&gt;(groop.QueueSobjects)).keySet()); } } } } public static Boolean isPrefix(Id sfdcId, String prefix) { if(sfdcId == null || prefix == null || prefix.length() != 3) { return false; } else { return String.valueOf(sfdcId).startsWith(prefix); } } private static testMethod void testGroupsHelper() { GroupsHelper helper = new GroupsHelper(); Integer rolesCount = [SELECT Id FROM UserRole].size(); System.assertEquals(rolesCount, helper.roleUsers.size()); System.assertEquals(rolesCount, helper.roleSubordinateUsers.size()); if(helper.usersQueues.size() &gt; 0) { Integer queueCount = [SELECT Id FROM QueueSobject].size(); Id userId = new List&lt;Id&gt;(helper.usersQueues.keySet())[0]; System.assertEquals(queueCount, helper.usersQueues.get(userId).size()); } //Can't test the size of the groups and users Maps because they only //contain a key if there are child values to add. } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload