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
Ranjith YMRanjith YM 

lead count

  List<Lead> numberOfLeads = [select Id from Lead];
   System.debug(numberOfLeads.size());

I need number of Leads but above my code is returning 0 always, please help me. thanks in advance
Best Answer chosen by Ranjith YM
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ranjith,

Greetings to you!

Are you running these statements in test class? By default, test methods cannot see your data. You need to insert leads in a test class or use @isTest(SeeAllData=true)

Also, if you are executing it in apex class then I guess that the most possible issue is if apex runs with sharing under a restricted user.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ranjith,

Greetings to you!

Are you running these statements in test class? By default, test methods cannot see your data. You need to insert leads in a test class or use @isTest(SeeAllData=true)

Also, if you are executing it in apex class then I guess that the most possible issue is if apex runs with sharing under a restricted user.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Jayakrishnan SalimJayakrishnan Salim
Your code should work. You can also try the below code to get the lead count using an aggregate function. 
 
AggregateResult[] groupedResults = [select count(Id) leadCount from Lead];
system.debug(groupedResults[0].get('leadCount'));

​​​​​​​
Deepali KulshresthaDeepali Kulshrestha

Hi Ranjith
 
Greetings to you!

I have tried your code in my org and it is working fine. So please check the leads in your org manually or post complete code to find the issue.

List<Lead> numberOfLeads = [select Id from Lead];
System.debug(numberOfLeads.size());
            
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com