I am trying to find a count of users in all public groups in a salesforce org. Has anybody done any apex class which can get the results? Buyan
2 Answers
Execute the below code. You will get the answer.
List<group> groupRecList = [SELECT Id, Name, DeveloperName FROM Group WHERE Type = 'Regular';
Set<Id> SetOfUsers = new Set<Id>();
for (GroupMember GrpMem : [Select GroupId, UserOrGroupId From GroupMember Where GroupId IN :groupRecList]){
if (GrpMem.UserOrGroupId.getSObjectType() == User.SObjectType)
SetOfUsers.add(GrpMem.UserOrGroupId);
}
system.debug('SetOfUsers==>'+SetOfUsers.size());
You can query the GroupMember
object. But don't forget to exclude Ids that start with the same prefix as groups (because groups can be referenced in other groups too).