4

Following query won't work if number of records is more than 50,000.

Integer c = [SELECT COUNT() FROM MyObj__C];

Is there a workaround to do calculation anyway?

  • where do you want to show count? – Mohith Shrivastava Feb 26 '14 at 8:57
  • on standard page. – Ganesh Bhosle Feb 26 '14 at 8:58
  • Is your requirement only to display count ?No DML ? – Mohith Shrivastava Feb 26 '14 at 8:59
  • @MohithKumar, I just need to do the calculation & store value in a variable. It will be displayed on standard page. – Ganesh Bhosle Feb 26 '14 at 9:00
  • @MohithKumar, yes only do the count & store in variable. – Ganesh Bhosle Feb 26 '14 at 9:01
5

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_controller_readonly_context.htm

Set the context to read Only .Read only on page will allow you to query for million records atleast

<apex:page controller="SummaryStatsController" readOnly="true">
   <p>Here is a statistic: {!veryLargeSummaryStat}</p>
</apex:page>

Controller code

public class SummaryStatsController {
public Integer getVeryLargeSummaryStat() {
    Integer closedOpportunityStats = 
        [SELECT COUNT() FROM Opportunity WHERE Opportunity.IsClosed = true];
    return closedOpportunityStats;
}
}
  • If i dont have an apex page, can i add @Readonly annotation to the method in class and call this class method from a class which implements batchable and does some delete dml operations. I need record count in a batch class which does dml delete. – Walker Dec 10 '15 at 11:25
1

I think, You have to write a batch class for this . In execute method, You can have static method and increase the count based on how many time methods get called. Please put Database.stateful to maintain the value of the variable. Thanks.

1

With the Summer '18 Release there was a new feature for this:

Basically, the COUNT() no longer contributes towards the query row limit (typically 50,000 records). Instead in counts once towards that limit. There is some additional consideration when using GROUP BY, but generally you can count a far greater number of records now.

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.