0

I am trying to build a map<String, List<Id>> where the List<Id> is the list of UserOrGroupId field in the GroupMember Object that has similar Group.DeveloperName values and that I wish to put in the key

I have a list of group Members List<GroupMember> groupMemberList And I am confused on how to fill my map when looping through the list of group members Any tips or hints on how I can this work ?

3

Iterate GroupMember records and group UserOrGroupId by unique Group.DeveloperName

List<GroupMember> groupMemebrs = [
    SELECT Id, GroupId, Group.DeveloperName, UserOrGroupId
    FROM GroupMember
    LIMIT 50000
    ];
Map<String, List<Id>> groupMemebersByGroupDeveloperName = new Map<String, List<Id>>();
for(GroupMember groupMember :groupMemebrs){
    List<Id> userGroupIds = new List<Id>();
    if(groupMemebersByGroupDeveloperName.containsKey(groupMember.Group.DeveloperName)){
        userGroupIds = groupMemebersByGroupDeveloperName.get(groupMember.Group.DeveloperName)
    }
    userGroupIds.add(groupMember.UserOrGroupId);
    groupMemebersByGroupDeveloperName.put(groupMember.Group.DeveloperName, userGroupIds);
}

result is stored in Map groupMemebersByGroupDeveloperName

2
  • But the groupMemebersByGroupDeveloperName is empty ! how are we supposed to check its key Sep 30 at 8:49
  • 1
    @ismailkartit It is populated by the loop; we create a new List or get the existing List, add the Id to the List, and then put it in the Map.
    – sfdcfox
    Sep 30 at 9:31

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.