+ Start a Discussion
Manohar kumarManohar kumar 

how to count assigned lead for users


i cant figre out how to use aggregate query for this.

i need to count assigned lead to a user. i believe assignment is judged by the ownerId. 

i need to do this using aggregate query.. 

i new to get map <userId, count(assinedLead)>, how do i get this.?

please let  me know how to do this,  i never used aggrregate query before.



Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Manohar Kumar,

May I request you to please refer the below link to a similar requirement. Hope it will be helpful.

Best Regards
Rahul Kumar
Shiva RajendranShiva Rajendran
Hi Manohar ,
Use the below code
Map<Id,Integer> AllLeadGroupedMap=new Map<Id,Integer>();
List<User> usersInOrg =[select id from user ];
     	map<Id,User>   userMap = new map<Id,User>(usersInOrg );  // userMap
		AggregateResult[] groupedResults  = [SELECT ownerId,count(id) totCount FROM Lead group by ownerId];
for(AggregateResult ag:groupedResults)

// System.debug(ii+'  '+ag.get('ownerId') +'  '+ag.get('totCount'));
   AllLeadGroupedMap.put(   (Id)ag.get('ownerId'),Integer.valueOf(ag.get('totCount')));
//AllLeadGroupedMap will contain the result as you expect
// use the below code to display the values
for(Id idd : AllLeadGroupedMap.keySet())
Let me know if you need further help.
Thanks and Regards,
Shiva RV