[SalesForce] GroupMember Types of a Public Group

How can we get the Type of a Group Member?

Using getSObjectType() on the UserOrGroupId of GroupMember we get the Types – but they are only User or Group.

List<GroupMember> members = [ SELECT Id, UserOrGroupId FROM GroupMember WHERE GroupId IN (SELECT id FROM Group where Id = :groupId) ];


for(GroupMember member: members) {
  System.debug(member.Id.getSObjectType() + '-' + member.UserOrGroupId.getSObjectType());


}

enter image description here

Any help/suggestions is appreciated.

Best Answer

Your code will look like this:

List<GroupMember> members = [ SELECT Id, GroupId, UserOrGroupId 
                            FROM GroupMember 
                            WHERE GroupId IN (SELECT id FROM Group where Id = :groupId) ];

List<Id> ids = new List<Id>();
for(GroupMember member: members) {
   ids.add(member.UserOrGroupId);
}
List<Group> lstGroup = [SELECT Name, RelatedId, Type, Id From Group WHERE Id IN:ids];

RelatedId will be the Type of Id. For Groups of type “Role,” the ID of the associated UserRole. The RelatedId field is polymorphic.

and Type will display as User, Role, Partner User etc as shown in your picture

Description Type of the Group. One of the following values:

Regular—Standard Public Group. When you create() a Group, its type must be Regular, unless a partner portal is enabled for the organization, in which case the type can be Regular or PRMOrganization.

Role—Public Group that includes all of the User records in a particular UserRole.

RoleAndSubordinates—Public Group that includes all of the User records in a particular UserRole and all of the User records in any subordinateUserRole.

Organization—Public Group that includes all of the User records in the organization. This Group is read-only.

Case—Public group of users, members of a queue that can own a Case.

Lead—Public group of users, members of a queue that can own a Lead.

Manager—Public group that includes a user’s direct and indirect managers. This Group is read-only.

ManagerAndSubordinatesInternal—Public Group that includes a user and the user’s direct and indirect reports. This Group is read-only.

PRMOrganization—Public Group that includes all of the partners in an organization that has the partner portal feature enabled.

Queue—Public Group that includes all of the User records that are members of a queue.

Territory—Public Group that includes all of the User records in an organization that has the territory feature enabled.

TerritoryAndSubordinates—Public Group that includes all of the User records in a particular UserRole and all of the User records in any subordinateUserRole.

Collaboration—Chatter group.

Only Regular, Case, and Lead can be used when creating a group. The other values are reserved.