[SalesForce] Getting error “System.LimitException: Too many query rows: 50001” what i do in this case.

i tried below resolutions but no any worked…

REsolution:

If your controller does not have any DML operation than you can easily solve above two salesforce governer limits using the attribute readOnly="true" by default it's value is false.

  1. Query Rows limits increased from 50001 to 1 million rows
  2. collection size limits increased from 1001 to 10,000

  3. // Return the number of ad book clients
    public string getClientAccountCount() {
    System.debug(LoggingLevel.Debug, 'getClientAccountCount() – Current Query Rows: ' + Limits.getQueryRows() + '/' + Limits.getLimitQueryRows());
    integer recordCount = [Select count() from Account where SomeCustomField__c = 'Client' limit 1001];
    if(recordCount == 1001) { return '1000+'; }
    return string.valueOf(recordCount);
    }

3.Integer limitRows = Limits.getLimitQueryRows() – Limits.getQueryRows();
2
List accnts = [select id from account limit :limitRows];

here is my code:

private List<AggregateResult> getTaskList() {
taskList = new List<AggregateResult>();
if(isFirstTime) {
  if(Test.isRunningTest()) {
    taskList = [Select count(id) total, Object_Name__c, WhatId  From Task t where (t.Status = 'In Progress' OR t.Status = 'Not Started') 
        AND t.Trial_Id__c =:asp.id AND t.WhatId != null and Subject != 'Resubmit Rejected CRF' and subject NOT IN ('Schedule the patient for a core biopsy', 'Schedule the patient for a blood sample', 'Schedule the patient for a MRI scan', 'Schedule a clinic appointment for Response Evaluation CRF','Schedule a clinic appointment after randomization', 'Schedule Treatment Phase', 'Schedule the patient for a lab visit', 'Schedule a clinic appointment for Followup CRF') 
        group by t.WhatId, t.Object_Name__c limit 1];
  } else {
    **taskList = [Select count(id) total, Object_Name__c, WhatId  From Task t where (t.Status = 'In Progress' OR t.Status = 'Not Started') 
        AND t.Trial_Id__c =:asp.id AND t.WhatId != null and Subject != 'Resubmit Rejected CRF' and subject NOT IN ('Schedule the patient for a core biopsy', 'Schedule the patient for a blood sample', 'Schedule the patient for a MRI scan', 'Schedule a clinic appointment for Response Evaluation CRF','Schedule a clinic appointment after randomization', 'Schedule Treatment Phase', 'Schedule the patient for a lab visit', 'Schedule a clinic appointment for Followup CRF') 
        group by t.WhatId, t.Object_Name__c ];**
  }
  isFirstTime = false;
  system.debug('isFirstTime: '+isFirstTime);
} else {
  string queryStrValue = 'Select count(id) total, Object_Name__c, WhatId From Task t ';
  if(selectedCRFViewOption == 'pending submission'){
    //queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND t.Trial_Id__c != null AND t.WhatId != null AND t.OwnerId = \''+UserInfo.getUserId()+'\' group by t.Object_Name__c ';
    queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND t.Trial_Id__c = \''+asp.id+'\' and Subject != \'Resubmit Rejected CRF\' AND t.WhatId != null and subject NOT IN (\'Schedule the patient for a core biopsy\', \'Schedule the patient for a blood sample\', \'Schedule the patient for a MRI scan\', \'Schedule a clinic appointment for Response Evaluation CRF\', \'Schedule a clinic appointment after randomization\', \'Schedule Treatment Phase\', \'Schedule the patient for a lab visit\', \'Schedule a clinic appointment for Followup CRF\') group by t.Object_Name__c, WhatId ';
    showTabPanel = true;
    system.debug('queryStrValue pending submission: '+queryStrValue);
  }else if(selectedCRFViewOption == 'pending re-submission'){
    //queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND Subject = \'Resubmit Rejected CRF\' AND CRF__c != null AND Trial_Id__c != null AND WhatId != null AND OwnerId = \''+UserInfo.getUserId()+'\' group by t.Object_Name__c ';
    //queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND Subject = \'Resubmit Rejected CRF\' AND CRF__c != null AND Trial_Id__c =\''+asp.id+'\' group by t.Object_Name__c, WhatId ';
    queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND Subject = \'Resubmit Rejected CRF\' AND Trial_Id__c =\''+asp.id+'\' group by t.Object_Name__c, WhatId ';
    showTabPanel = true;
  }else if(selectedCRFViewOption == 'Patients pending registration'){
    //queryStrValue = queryStrValue + 'where Object_Name__c = \'Registration__c\' AND (Status = \'In Progress\' OR Status = \'Not Started\') AND Trial_Id__c != null AND WhatId != null AND OwnerId = \''+UserInfo.getUserId()+'\' group by t.Object_Name__c ';
    queryStrValue = queryStrValue + 'where Object_Name__c = \'Registration__c\' AND (Status = \'In Progress\' OR Status = \'Not Started\') AND Trial_Id__c =\''+asp.id+'\' AND WhatId != null group by t.Object_Name__c, WhatId ';
    showTabPanel = false;
    crfFormName = CRFFormsMap.get('Registration__c');
    crfFormApiName = CRFFormTypeMap.get(crfFormName);
    Apexpages.currentPage().getParameters().put('CRFName','Registration__c');
    CRFPendingListsView();

  }else if (selectedCRFViewOption == 'Delayed CRFs'){
    //queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND t.Trial_Id__c != null AND t.WhatId != null AND t.OwnerId = \''+UserInfo.getUserId()+'\' AND t.Original_Due_Date__c < TODAY group by t.Object_Name__c ';
    queryStrValue = queryStrValue + 'where (t.Status = \'In Progress\' OR t.Status = \'Not Started\') AND t.Trial_Id__c =\''+asp.id+'\' AND t.WhatId != null AND t.Original_Due_Date__c < TODAY and subject NOT IN (\'Schedule the patient for a core biopsy\', \'Schedule the patient for a blood sample\', \'Schedule the patient for a MRI scan\', \'Schedule a clinic appointment for Response Evaluation CRF\', \'Schedule a clinic appointment after randomization\', \'Schedule Treatment Phase\', \'Schedule the patient for a lab visit\', \'Schedule a clinic appointment for Followup CRF\') group by t.Object_Name__c, WhatId ';
    showTabPanel = true;  
  }else{
    System.debug('selectedCRFViewOption--------else----'+selectedCRFViewOption);
    queryStrValue = queryStrValue + 'where (Status = \'In Progress\' OR Status = \'Not Started\') AND Object_Name__c = \''+selectedCRFViewOption+'\' AND Trial_Id__c != null AND WhatId != null group by t.Object_Name__c, WhatId ';
    showTabPanel = false;
    crfFormName = CRFFormsMap.get(selectedCRFViewOption);
    crfFormApiName = CRFFormTypeMap.get(crfFormName);
  }

  System.debug('queryStrValue--------------'+queryStrValue);
  if(Test.isRunningTest()) {
    queryStrValue+= ' limit 1';
  }
  taskList = Database.query(queryStrValue);
}

System.debug('taskList-----------'+taskList);

return taskList;
  } 

Please help me out in this….bold soql is where i am getting error….

Best Answer

If your controller does not have any DML operation than you can easily solve above two salesforce governer limits using the attribute readOnly="true" by default it's value is false.

  1. Query Rows limits increased from 50001 to 1 million rows
  2. collection size limits increased from 1001 to 10,000
Related Topic