Accessing User from a Public Group from Apex.
I came across a requirement where i have to send the email to a specific group of User. The specific user is to be identified by the users present in a public group. For this i have to build logic to find all the users in a public group. Then I realize logic to access all the User associated with a public group is not a straight forward. The complexity lies in the way SFDC maintain the group members. As we all know along with users a public group can also have Role, Role
and subordinates or another public group.
Case 1 : Public Group with Users as Group Member.
To access the list of users who are member of a Public Group, we need to query a standard object "GroupMemeber" all the member of the group are maintained this object.
Query to pull the group member of group with id '00G18000000Q443'
1 2 3 4 5 6 | List<Id> usersIdsInGroup= new List<Id>(); List<GroupMember> groupMembers=[Select Id, group.type, UserOrGroupId From GroupMember where group.id='00G18000000Q443']; for(GroupMember gm : groupMembers) { usersIdsInGroup.add(gm.UserOrGroupId); } |
Case 2 : Public Group with Users and Role as Group Member.
The role added to the
public group are maintained as separate (sub) groups by SFDC. Hence to find
which Role is been added to the public group we need to query the sub
group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | String userType = Schema.SObjectType.User.getKeyPrefix(); String groupType = Schema.SObjectType.Group.getKeyPrefix(); List<Id> usersIdsInGroup= new List<Id>(); for(GroupMember gm : [SELECT Id, group.name, group.type, UserOrGroupId FROM GroupMember where group.id='00G18000000Q443']) { if(gm.UserOrGroupId.startsWithIgnoreCase(userType)) { usersIdsInGroup.add(gm.UserOrGroupId); } else if(gm.UserOrGroupId.startsWithIgnoreCase(groupType)) { /*This section is to handle the Roles added to the public group. To get the Role Id added we need to Query the sub group & relatedId gives the Role ID. */ List<Group> groupWithRole=[Select Id, RelatedId From Group where Id=:gm.UserOrGroupId]; if(groupWithRole[0].RelatedId!=null) { List<User> roleUsers= [Select Id from User where UserRole.Id=:roleId]; for(User tempUser : roleUsers) { usersIdsInGroup.add(tempUser.ID); } } } } |
Case 3 : Public Group with Users and another public group as group member
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | String userType = Schema.SObjectType.User.getKeyPrefix(); String groupType = Schema.SObjectType.Group.getKeyPrefix(); List<Id> usersIdsInGroup= new List<Id>(); for(GroupMember gm : [SELECT Id, group.name, group.type, UserOrGroupId FROM GroupMember where group.id='00G18000000Q443']) { if(gm.UserOrGroupId.startsWithIgnoreCase(userType)) { usersIdsInGroup.add(gm.UserOrGroupId); } else if(gm.UserOrGroupId.startsWithIgnoreCase(groupType)) { /* This section is to handle the public group added to the public group. * To get the User of sub public group, we need to query the sub query. */ for(GroupMember subGroup_gm : [SELECT Id, group.name, group.type, UserOrGroupId FROM GroupMember where group.id=:gm.UserOrGroupId]) { usersIdsInGroup.add(subGroup_gm.UserOrGroupId); } } } |
Case 4: Public Group with Users and Role & Subordinates as
group member.
This Complex the logic to peak. There are n number of loops possible in this case. The hint to have this logic to use "parentRoleID" of the Role object to identify the tree structure of the Role.
No comments:
Post a Comment