I created a Public Group and added Roles as members. When I am querying the GroupMembers of that Group, I am getting the Ids for the Groups with Type Role. But when I am trying to query the Users/GroupMembers from the returned Group of type Role, no records are being returned.Here I have shared the class used to query members of a group and the result when we are passing a set of Ids of Groups with Type Role.

      Public class PublicGroupUtil{
      public static Set<Id> findUsersFromGroup(Set<Id> PublicGroupSet) {
         set<Id> userSet = new set<Id>();
         Set<Id> groupIdSet = new Set<Id>();
         String userType = Schema.sObjectType.User.getKeyPrefix();
         Map<Id,Group> groupMap = new Map<Id,Group>([select Type from Group where id in:PublicGroupSet]);
         for(GroupMember member : [select UserOrGroupId from GroupMember where groupId =:PublicGroupSet]) {
            if(((String)member.UserOrGroupId).startsWith(userType)) {
         if((groupIdSet.size() > 0) && (Limits.getLimitQueries() > Limits.getQueries())) {
        return userSet;

I have then passed a set of Group Ids whose type is Role to the method:

Set<Id> lstOfGroupIds = new Set<Id> ();
for(Group g: [SELECT Id FROM Group WHERE Type = 'Role']){
System.debug('Role Type Groups size = '+lstOfGroupIds.size());
Set<Id> lstOfGroupMembers = new Set<Id> ();
lstOfGroupMembers = PublicGroupUtil.findUsersFromGroup(lstOfGroupMembers);
System.debug('GroupMembers size = '+lstOfGroupMembers.size());

The debug log gives the following result:

Execute Anonymous: Set<Id> lstOfGroupIds = new Set<Id> ();
Execute Anonymous: for(Group g: [SELECT Id FROM Group WHERE Type = 'Role']){
Execute Anonymous: lstOfGroupIds.add(g.Id);
Execute Anonymous: }
Execute Anonymous: System.debug('Role Type Groups size = '+lstOfGroupIds.size());
Execute Anonymous: Set<Id> lstOfGroupMembers = new Set<Id> ();
Execute Anonymous: lstOfGroupMembers = PublicGroupUtil.findUsersFromGroup(lstOfGroupMembers);
Execute Anonymous: System.debug('GroupMembers size = '+lstOfGroupMembers.size());
Execute Anonymous: 
Execute Anonymous: 
Execute Anonymous: 
11:20:54.35 (35788466)|USER_INFO|[EXTERNAL]|005p0000000rIO3|csagnik@test.dev1|India Standard Time|GMT+05:30
11:20:54.35 (35800642)|EXECUTION_STARTED
11:20:54.35 (35804006)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
11:20:54.35 (36552803)|SOQL_EXECUTE_BEGIN|[2]|Aggregations:0|SELECT Id FROM Group 
11:20:54.35 (41823877)|SOQL_EXECUTE_END|[2]|Rows:198
11:20:54.35 (49590441)|USER_DEBUG|[5]|DEBUG|Role Type Groups size = 198
11:20:54.35 (49656145)|SYSTEM_MODE_ENTER|false
11:20:54.35 (73432689)|SOQL_EXECUTE_BEGIN|[15]|Aggregations:0|SELECT Type FROM Group WHERE id = :tmpVar1
11:20:54.35 (76354679)|SOQL_EXECUTE_END|[15]|Rows:0
11:20:54.35 (76623163)|SOQL_EXECUTE_BEGIN|[17]|Aggregations:0|SELECT UserOrGroupId FROM GroupMember 
11:20:54.35 (79427727)|SOQL_EXECUTE_END|[17]|Rows:0
11:20:54.35 (79614953)|SYSTEM_MODE_EXIT|false
11:20:54.35 (79672434)|USER_DEBUG|[8]|DEBUG|GroupMembers size = 0
11:20:54.79 (79707263)|CUMULATIVE_LIMIT_USAGE
11:20:54.79 (79707263)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 3 out of 100
  Number of query rows: 198 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:20:54.79 (79707263)|CUMULATIVE_LIMIT_USAGE_END

11:20:54.35 (79740205)|CODE_UNIT_FINISHED|execute_anonymous_apex
11:20:54.35 (81459019)|EXECUTION_FINISHED

Why does the query not return any records?

  • I'm wondering if this is a recent change in behaviour within Salesforce. I'm discovering the same thing today, but various guidance out on the web indicates that this should work. Did you discover a workaround? – Bigears Jul 26 '17 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.