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
Manohar kumarManohar kumar 

how to get map<user, List<lead>>()

Hi Team,

i am trying to get map<user, List<lead>>(), List of lead is records created by the user. I think query for getting related list will not work here.

How do i achieve this.

Thanks,

Manohar

Best Answer chosen by Manohar kumar
Shiva RajendranShiva Rajendran
Hi Manohar ,
Sorry there is no Map.contains() function
Use
AllLeadGroupedMap.containsKey(indLead.createdById).
I have verified the code.It is working fine.
Let me know if you need further help.
Thanks and Regards,
Shiva RV

All Answers

Shiva RajendranShiva Rajendran
Hi Manohar,

Using of Sobject as the key is not generally appreciated approach ,better use userId.
Do go through the following link if you want to use that pattern
Sobject map key  (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject_considerations.htm)

I have used user Id in here instead of using User as Key.
But i have created a map of user with id , so user details can be obtained easily instead of querying
public static Map<Id,List<Lead>> LeadMapReturnFunction()
{
      Map<Id,List<Lead>> AllLeadGroupedMap=new Map<Id,List<Lead>>(); 
      List<User> usersInOrg =[select id from user limit 5];
     map<Id,User>   userMap = new map<Id,User>(usersInOrg );  // userMap
     List<Lead> allLeads=[select id,name from lead where CreatedById in: usersInOrg]; //allLeads
     for(Lead indLead : allLeads)
     {
              List<Lead> leadArray;
             if(AllLeadGroupedMap.contains(indLead.createdById)) 
             {
                     //if key is already present , get the list and add the element to the list
                 leadArray=AllLeadGroupedMap.get(indLead.createdById);
                  leadArray.add(indLead);
             }
            else
          {
               //if key doesnt exist create a new list and add the new value into that list
                leadArray=new List<Lead>();
                 leadArray.add(indLead);

           }   

          AllLeadGroupedMap.put(indLead.createdById,leadArray); 
      }


       return AllLeadGroupedMap;

}

Let me know if you need further help.

Thanks and Regards,
Shiva RV
 
Manohar kumarManohar kumar

Hi Shiva,

Thanks for the reply. When i try to run that code in my developer console. it gave me some error. Can't figure out why.

Map<Id,List<Lead>> AllLeadGroupedMap=new Map<Id,List<Lead>>(); 
      	List<User> usersInOrg =[select id from user ];
     	map<Id,User>   userMap = new map<Id,User>(usersInOrg );  // userMap
		set<id> usersid = new set<id>();
		for(user u1:usersInOrg) {
    		usersid.add(u1.id);
		}
		List<Lead> allLeads=[select id,name,createdById from lead where createdById in: usersid]; //allLeads
		system.debug(':::'+allLeads);
     	for(Lead indLead : allLeads) { 
        	List<Lead> leadArray;
            
        	if(AllLeadGroupedMap.contains(indLead.createdById)) { 
                     //if key is already present , get the list and add the element to the list
                 leadArray=AllLeadGroupedMap.get(indLead.createdById);
                 leadArray.add(indLead);
            }
            
            else {
               //if key doesnt exist create a new list and add the new value into that list
            	leadArray=new List<Lead>();
                leadArray.add(indLead);
           	} 
			
			AllLeadGroupedMap.put(indLead.createdById,leadArray); 
        }															
	   system.debug('AllLeadGroupedMap::'+AllLeadGroupedMap);


ran through execute anonymous, error is 

 

Method does not exist or incorrect signature: void contains(Id) from the type Map&lt;Id,List&lt;Lead&gt;&gt;

Thanks,

Manohar

Shiva RajendranShiva Rajendran
Hi Manohar ,
Sorry there is no Map.contains() function
Use
AllLeadGroupedMap.containsKey(indLead.createdById).
I have verified the code.It is working fine.
Let me know if you need further help.
Thanks and Regards,
Shiva RV
This was selected as the best answer