+ Start a Discussion
Ranjith YMRanjith YM 

count number of account without lead

I can get the count of leads which has an account based on ConvertedAccountId/ConvertedContactId but I totally stuck to get the count of an Account which has no corresponding lead.

Can someone please help me with this... thanks ini advance

Deepak_KumarDeepak_Kumar
Hi Ranjith, Please try the following approach:
List<Lead> leads = new List<Lead>();
leads= [SELECT ConvertedAccountId FROM Lead WHERE ConvertedAccountId !=null];
Set<Id> ids = new Set<Id>();
for(Lead lead:leads){
    ids.add(lead.ConvertedAccountId);
}
List<Account> accounts = new List<Account>();
accounts= [SELECT Name FROM Account WHERE Id NOT IN :ids];
System.debug(accounts.size());

Please also mark this solve if this helps you.
Thanks.
Deepak