Last active
April 27, 2022 14:30
-
-
Save emoran/4ea945ec3db32cc8c06b to your computer and use it in GitHub Desktop.
Adding list values to apex map<String,List<Sobject>>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<Id, List<Id>> userRoleToUsers = new Map<Id, List<Id>>(); | |
for(User newUser : [SELECT UserRoleId FROM User LIMIT 50000]) { | |
if(userRoleToUsers.containsKey(newUser.UserRoleId)) { | |
List<Id> usersId = userRoleToUsers.get(newUser.UserRoleId); | |
usersId.add(newUser.Id); | |
userRoleToUsers.put(newUser.UserRoleId, usersId); | |
} else { | |
userRoleToUsers.put(newUser.UserRoleId, new List<Id> { newUser.Id }); | |
} | |
} |
Hi @emoran, can you please elaborate as of how can we use the list values in Map<Id, List> to create a new record for each id in the List in the Map?
Hi @Lorenzo8912 @pathakarchit, sorry for leaving this, I will create a small sample and comment the code for better undesrtanding.
I think we can write above code as below:
Map<Id, List<Id>> userRoleToUsers = new Map<Id, List<Id>>();
for(User newUser : [SELECT UserRoleId FROM User LIMIT 50000]) {
if(userRoleToUsers.containsKey(newUser.UserRoleId)) {
userRoleToUsers.get(newUser.UserRoleId).add(newUser.Id);
} else {
userRoleToUsers.put(newUser.UserRoleId, new List<Id> { newUser.Id });
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it the same?