I wrote below line in apex to get the number of leads which are assigned to each lead owners,

        String userID = activeLO.Id;
        List<Lead> numberOfLeads = [select Id from Lead where OwnerId =:userID and IsConverted=false];
        System.debug(numberOfLeads.size()+' Leads on LO:: ' + userID);

but its returning 0 always for each owner, even if they assigned with 100+ Leads.

  • Is there a reason you are not using a COUNT query? – Phil W Aug 20 '19 at 9:33
  • Hello Ranjit. Welcome to SE. On which event you are firing your code? after insert or any other event? I guess, activeLO is User object. – sfdc_sk Aug 20 '19 at 9:34
  • List<Lead> numberOfLeads = [select Id from Lead]; System.debug('count'+numberOfLeads.size()); Even this returning 0 for me... please someone help with this – Ranjit Aug 20 '19 at 12:40

It was returning always zero since i was running with @isTest so my test case was failing to see all the data, when i changed to @isTest(SeeAllData=true) it started working well and fine. thanks for all your response

| improve this answer | |
  • 3
    Using seeAllData=true is not good practice, and I would not recommend it to anyone (outside of some very rare circumstances that few people encounter). If this is part of a unit test, that's information you should have told us in your question. Best practice is to create all of the data you need for a test inside of the unit test class itself. One of the benefits is that you aren't relying on certain records being present (or not present) in the org that you're running your test in. Not following this advice can lead to situations where tests pass in one environment but not another. – Derek F Aug 20 '19 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.