+ Start a Discussion
JBabuJBabu 

to populate data in to Map<Id, List<Id>>

Hi,

 

Can some one give an example of how to populate data in to:

 

Map<Id,List<Id>>

 

Here Id is UserRoleId and List<Id> is the List of users who belong to that role id.

 

Thanks,

JBabu.

Best Answer chosen by Admin (Salesforce Developers) 
Eugene PozniakEugene Pozniak

Think, it will look something like this:

 

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 });
	}
}

 

 

All Answers

Eugene PozniakEugene Pozniak

Think, it will look something like this:

 

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 });
	}
}

 

 

This was selected as the best answer
SamHowleSamHowle
Thank you @Eugene! Huge help for me.