7

Trying to get a count of records(records more than 50000 in object) in one object through apex class(Getting limit Exception). Looking for any alternate solution.

I used database.countQuery()statement, but still I am getting records limit exception.

Any one give me alternate solution to get records count.

My query is dynamic query like:

String s = 'Case';
String soql = 'SELECT COUNT() FROM' + s;
Integer cnt = Database.countQuery(soql);
5
  • What was your query? In the developer console I just ran select count(id) from Lead in the query editor and it returned 433316.
    – Mike Chale
    Commented Sep 12, 2013 at 12:45
  • My query is dynamic query like String s = 'case'; String soql='select count() from'+s '; Integer cnt=database.countQuery(soql); This is all my code.
    – raghusfdc
    Commented Sep 12, 2013 at 12:49
  • Requirement like this. I want to list out object name and count of its corresponding records in object, that's why i iterating loop with object names and executing dynamic query inside the loop for all objects.
    – raghusfdc
    Commented Sep 12, 2013 at 13:02
  • Have you checked out the storage usage section in setup? It lists out the number of records for each object. Commented Sep 12, 2013 at 22:05
  • Yes I did checked storage usage, in that one custom object has 130000 records it is showing. Can we access storage usage data in to apex class? I checked storage usage accessibility in schema level but did't get any reference to access in apex class.
    – raghusfdc
    Commented Sep 13, 2013 at 9:22

2 Answers 2

6

Why shouldn't you use database.query(query) instead.

AggregateResult results = database.query('SELECT COUNT(id) result FROM someSObject');

System.debug('No of ids are: '+results.get('result')); // Id are always unique

Using database.countQuery() or [SELECT count() FROM someObject]; hits governor limit if records > 50000

If you can use @readonly in you class then that limit will extend to million.

Also try to filter it with some criteria using WHERE clause, field which have no significant of counting if they have some particular value that doesn't matter.

3
  • does the code you highlighted works if number of records returned are more than 50000 ? will the below work for more than 50000 rows : AggregateResult results = database.query('SELECT COUNT(id) result FROM someSObject'); System.debug('No of ids are: '+results.get('result')); // Id are always unique
    – Walker
    Commented Dec 10, 2015 at 9:55
  • 1
    @Walker for records > 50000 it won't work. You can use @ readonly attribute on page to extend this limit.
    – Ashwani
    Commented Dec 10, 2015 at 10:13
  • i need to use such queries in a class which implements Batchable. this batch class will be scheduled to run at specific intervals. Is there any relaxation in terms of number of records returned in case SOQL is being called in batch context ?
    – Walker
    Commented Dec 10, 2015 at 10:15
-4

creat a list for the records li and count them as count = li.size(); take one outputField in visualfroce page.

try this

1
  • Could you please add more useful information? Your answer is very unclear at the moment. Thanks. Commented Dec 2, 2013 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .