0

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

1

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());
0

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).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.