function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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.
Sumit ShelarSumit Shelar

Hi,

I have a question here.

As we all know, different roles will have different users. Suppose for the role 'A' we have 5 users. When code get executed then our map userRoleToUsers will be in the below format. (Id of the role -> ('Id of 1st user', 'Id of 2d user'....etc)).

 

Now what should I do if I want to make them owner of the record alternately?  Like 1st - 2nd- 3rd - 4th - 5th then agin 1st?