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?
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?
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;
}
}
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.
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.
Use the countQuery it does not count against that limit
I know that here is 7 years old but nobody really answered it without using a class and VF
sample works in dev-console and returns for me USER_DEBUG [1]|DEBUG|184880 system.debug(Database.countQuery('select count() from Asset'));