-3

I'm getting a "Too Many Query Row 50001" error even if I have a limit of 1000. Want to know what I'm doing wrong or clause I am missing.

public Task[] getListTasks() {
  List<Contact> cntct = [SELECT Id, Name FROM Contact];
  List<Id> contactIds = new List<Id>();

  for(Contact cn : cntct) {
   contactIds.add(cn.Id);
  }

  taskList = 'select whoid,subject,status,LastModifiedDate, WhatID, Description, Primary_Contact_Phone_Number__c, Primary_Contact_Email_Address__c, Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c from task where CreatedbyId=\''+currentUser+'\'order by LastModifiedDate asc limit 1000 WHERE CreatedDate > 2013-06-11T10:00:00-08:00';

  //delete duplicate property records
  List<Task> tskList = Database.query(taskList);

  for (Integer d = 0; d < tskList.size(); d++){
    String checkWhat=tskList[d].WhatID;

    for (Integer i = 0; i < tskList.size(); i++){
      if(tskList[i].WhatID == checkWhat){
        String debugV=tskList[i].WhatID;
        tskList.remove(i);
      }
      else{
        String debugV=tskList[i].WhatID;
      }
    }
  } 

  return tskList;     
}

public void reset(){
  taskList = 'select whoid,subject,status,LastModifiedDate, WhatID, Description, Primary_Contact_Phone_Number__c, Primary_Contact_Email_Address__c, Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c from task where id !=null   order by LastModifiedDate desc Limit 1000';   
  List<Task> tskList = Database.query(taskList);  
}

Can you guys help out?

8
  • 1
    It is a transaction limit, every query row gets added up. How many Contact records do you have?
    – Adrian Larson
    Commented Dec 17, 2015 at 7:36
  • Also your looping seems remarkably inefficient. What exactly are you trying to accomplish?
    – Adrian Larson
    Commented Dec 17, 2015 at 7:39
  • @AdrianLarson I would say over 80k.
    – Ninja King
    Commented Dec 17, 2015 at 7:39
  • 1
    Well your query against them doesn't even have a WHERE clause so you're going to get all of them...
    – Adrian Larson
    Commented Dec 17, 2015 at 7:40
  • @AdrianLarson the goal is to get a list of Properties associated with contacts last call date. The loop inside will get the last modified date if the subject is x.
    – Ninja King
    Commented Dec 17, 2015 at 7:42

2 Answers 2

3

This probably isn't all of the problem, but at least in part, your SOQL statements are out of order, and that might be why the LIMIT isn't having any effect on at least the task query. Adrian's right though, your code is likely failing before it even hits that, at the contacts query because you don't have either a LIMIT or a WHERE clause to narrow down the number of returned records.

It should be:

task = 'SELECT WhoId, Subject, Status, LastModifiedDate, WhatId, Description, Primary_Contact_Phone_Number__c, Primary_Contact_Email_Address__c, Call_Result__c, Last_Call_Date__c, Last_Call_Agent__c FROM Task WHERE CreatedbyId=\''+currentUser+'\' AND  CreatedDate > 2013-06-11T10:00:00-08:00 ORDER BY LastModifiedDate ASC LIMIT 1000';

Also: I'm going to "suggest an edit" and clean up your post if that's alright. Remove the debugs, commented out lines, and re-indent it so it's easier for everyone to read.

2
  • I edited that list you had and also added a limit on... List<Contact> cntct = [SELECT Id, Name FROM Contact limit 20000];
    – Ninja King
    Commented Dec 17, 2015 at 20:44
  • That's great, but Adrian and Andree's comments are important to implement. What I suggested is a matter of syntax and may keep it from breaking in that spot, but if you throw a LIMIT onto a query, you're potentially omitting records you need to effect. So if your goal is to have this check all records that it may apply to in the system and you have more than 50k, then use Andree's suggestion. If you don't need to check them all, then Adrian's suggestion of filtering your query by adding a WHERE clause instead of using LIMIT to try and get the total number under 50k should do it.
    – KB145
    Commented Dec 17, 2015 at 21:06
1

the only way to fetch more than 50000 rows it to move your code to a batch class and then call this batch class from within your code.

The batch documentation says:

For example, a batch Apex job for the Account object can return a QueryLocator for all account records (up to 50 million records) in an org.

Here you can get some further information about apex batch https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

You must log in to answer this question.

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