function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

System.LimitException: Too many query rows: 50001

I'm suddenly getting this error when pushing unrelated code to production. The class in question has been working fine for over a year. The error lines are anywhere it says tstcont.submitCase();

class TestWeb2CaseCont {
    static testMethod void testCaseWithContact(){
        Account a = TestUtils.getAccount('ut Acc1');
        insert a;
        Contact ct = TestUtils.getContact(a, 'ut Contact1');
        ct.Email = '';
        insert ct;
        Web2CaseCont tstcont = new Web2CaseCont();
        tstcont.c.SuppliedEmail = ct.Email;
        System.assertEquals(ct.Id, tstcont.c.ContactId);
        System.assertEquals(a.Id, tstcont.c.AccountId);
    static testMethod void testCaseWithoutContact(){       
        Web2CaseCont tstcont = new Web2CaseCont();
        System.assertEquals(tstcont.c.ContactId, null);
        System.assertEquals(tstcont.c.AccountId, null);
    static testMethod void testPageParameters(){       
        Test.setCurrentPageReference(new PageReference('Page.Web2Case'));
        System.currentPageReference().getParameters().put('pline', 'None');
        System.currentPageReference().getParameters().put('Origin', 'None');
        System.currentPageReference().getParameters().put('co', 'None');       
        Web2CaseCont tstcont = new Web2CaseCont();
        System.assertEquals(tstcont.c.Product_Line__c,'None' );
        System.assertEquals(tstcont.c.Origin,'None' );
        System.assertEquals(tstcont.c.SuppliedCompany,'None' );
    static testMethod void testAttachment(){       
        Attachment att = new Attachment();
        att.Name = 'ut attachment';
        att.Body = Blob.valueOf(att.Name);
        Web2CaseCont tstcont = new Web2CaseCont();
        tstCont.att = att;
    static testMethod void testFailure(){          
        Web2CaseCont tstcont = new Web2CaseCont();
        tstcont.c = null;
        System.assertEquals(tstcont.msg, System.label.WebCaseFailure );
That is an SOQL error. Your SOQL statement is trying to access too many records.
I don't see any SOQL in this code, so it must be in tstcont.submitCase();

Why now and not before? Because you likely have more records now then when the program was developed, and the original development did not take into consideration the SFDC limits.
public PageReference submitCase() {
        c.SuppliedName = c.SuppliedFirstName__c + ' ' + c.SuppliedLastName__c;
          List<Contact> cnt = [Select Id, Account.Id from Contact where Email =: c.SuppliedEmail];
          if((!cnt.isEmpty()) && (cnt.size() == 1)){
              c.ContactId = cnt.get(0).Id;
              c.AccountId = cnt.get(0).Account.Id;

How can I limit the number of records returned here?
Rahul SharmaRahul Sharma
You should add a limit less than 50000 in your SOQL, 40k for being on safer side.
List<Contact> cnt = [Select Id, Account.Id from Contact where Email =: c.SuppliedEmail LIMIT 40000];
Do you have the seeAllData=true for the test classes?