This question already has an answer here:

I am getting too many query rows 50001 error while running this class method from execute anonymous.

I got same error when annotating execute method in scheduled class with readonly annotation as well. This is where i will be using this in real scenario.

I was expecting i should be able to count upto 1 million rows using readonly. I need to count more than 50000 records.

What am i doing wrong :

public class Test_Aggregate {
    public static void TestAgg() {
        List<Custom_Object__c> custList = [SELECT Id  FROM Custom_Object__c];
        System.debug('No of records are: '+custList.size());


why did the below not work as well and threw same SOQL row limit 50000 row error :

AggregateResult logResults = database.query('SELECT COUNT(id) result FROM Custom_Object__c');
integer recCount = Integer.valueOf(logResults.get('result'));
System.debug('No of Log records are: '+recCount);

Why does salesforce documentation says : The @ReadOnly annotation allows you to perform unrestricted queries against the Force.com database. It appears misleading. I am not doing any DML etc.

All i am asking is for a simple statistic about my object which stores my data. These statistic seem to be gathered every 24 hours. Why platform does not provide any API so that it can be easily accessed. I have heard of hacks where people scan the html of setup page to get it. Why is platform so limiting that i cant get it without running into limits.

marked as duplicate by dphil, cropredy, crmprogdev, superfell, Adrian Larson Dec 11 '15 at 22:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • my question is what is wrong with the way i wrote code and as expected why did 50000 rows for SOQL query rows to one million limit not got relaxed for my code even after using readonly. – Walker Dec 11 '15 at 14:32

This question is duplicate still providing the same answer

Looks like you are still confuse. If you use in for loop. It will not process complete records (i.e. more than 50,000). For loop executes in chunks i.e. 200.

public class Test_Aggregate{
        public static void TestAgg() {
            Integer intCount = 0;

            for(AggregateResult result :[SELECT COUNT(Id) intVal FROM Custom_Object__c])

            System.debug('No of records are: '+intCount);


@ReadOnly only work with JS remoting. If you direct call the method using class instance then it wouldn't work

Here is the example(IN this example I have 97000 Task records.)

<apex:page controller="TestVFpage">

    <div id="totalAccounts"></div> 
                            alert('Total Records: '+result.length);



public class TestVFpage {
    public static list<Task> getTask(){
        return [SELECT Id FROM Task];

alert message enter image description here

Another way to count records.

   <input name="getList" class="btn" value="Get Account list" type="button" onclick="getSobjectList()" />
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      var $j = jQuery.noConflict();
      var sessionId = '{!$Api.Session_ID}';
      function getSobjectList() {
      type: "GET",
      url: "/services/data/v35.0/query?q="+''+"{!URLENCODE('SELECT count(Id) totalTask FROM Task')}",
      headers: {"Authorization" : "OAuth " + sessionId},
      crossDomain : true,
      dataType: 'application/json',
      success: function (responseData) {
      error: function (request, status, error) {

check browser console

enter image description here

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