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
martinjhmartinjh 

Visualforce Page Error: Too many query rows: 50001 - but SOQL query returns only 105 rows

Hi folks,

 

I've inherited a series of Visualforce pages and up until yesterday they were working but now all of a sudden I'm getting the following error:

 

Too many query rows: 50001 

 

An unexpected error has occurred. Your development organization has been notified.

 

I've used the Developer Console and it's pin pointed the FATAL_ERROR in the Execution Log to the following method:

 

global class mForceSupportCustCaseRptGeneratorCtrl {

    String customerID;
    String reportMonth;    
    String reportYear;
    
    public PageReference generateReport() {
        PageReference reportPage = Page.mForceSupportCustCaseReport;
        reportPage.setRedirect(true);
        reportPage .getParameters().put('m', reportMonth);
        reportPage .getParameters().put('y',  reportYear);
        reportPage .getParameters().put('acc', customerID);
        return reportPage ;
    }
    
    public List<SelectOption> getListcustomeroptions() {

        List<AggregateResult> customerlist = [Select Account.Name accname, Accountid , count(createddate) FROM Case group by Account.Name, Accountid order by Account.Name limit 25000];

        List<SelectOption> options = new List<SelectOption>();
        
        Map<string,string> accountmap = new Map<string,string>();
        
        for (AggregateResult ar : customerlist ) {
            if (ar.get('expr0')!=null && ar.get('accname')!= null)
                accountmap.put(string.valueof(ar.get('Accountid')),string.valueof(ar.get('accname')));
        }
 
        for(String accid: accountmap.keyset()) {
            options.add(new SelectOption(accid, accountmap.get(accid)));
        }

        return QuickSort.sortOptionList(options);
    }
 
    public String getCustomerID() {
        return customerID;
    }
 
    public void setCustomerID(String customerID) {
        this.customerID = customerID;
    }    

    public List<SelectOption> getlistmonthoptions() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('1', 'January'));
        options.add(new SelectOption('2', 'February'));
        options.add(new SelectOption('3','March'));
        options.add(new SelectOption('4','April'));
        options.add(new SelectOption('5','May'));
        options.add(new SelectOption('6','June'));
        options.add(new SelectOption('7','July'));
        options.add(new SelectOption('8','August'));
        options.add(new SelectOption('9','September'));
        options.add(new SelectOption('10','October'));
        options.add(new SelectOption('11','November'));
        options.add(new SelectOption('12','December'));
        return options;
    }
 
    public String getReportMonth() {
        return reportMonth;
    }
 
    public void setReportMonth(String reportMonth) {
        this.reportMonth= reportMonth;
    }    

    public List<SelectOption> getlistyearoptions() {

        //List<AggregateResult> minmaxdates = [Select MIN(createddate) mindate, MAX(createddate) maxdate FROM Case limit 25000];
        //Integer minyear = datetime.valueof(minmaxdates.get(0).get('mindate')).year();
        //Integer maxyear = datetime.valueof(minmaxdates.get(0).get('maxdate')).year();

        List<SelectOption> options = new List<SelectOption>();
        
        for (Integer i = datetime.now().year() ; i>= 2009 ;i--) {
            options.add(new SelectOption(string.valueof(i),string.valueof(i)));
        }
        return options;
    }
 
    public String getReportYear() {
        return reportMonth;
    }
 
    public void setReportYear (String reportYear) {
        this.reportYear= reportYear;
    }    

}

 Looking at the Execution Log within the Developer Console I can see the (one and only) SOQL statement in the method being executed:

 

List<AggregateResult> customerlist = [Select Account.Name accname, Accountid , count(createddate) FROM Case group by Account.Name, Accountid order by Account.Name limit 25000];

 This obviously has the "limit" keyword on it so should be limiting the resultset, plus in the Developer Console I can see SOQL_EXECUTE_END [18] Rows:105

 

So I'm more than a little confused as to why it thinks the limit has been exceeded and any help would be much appreciated.

 

Thanks,

Martin

 

raseshtcsraseshtcs

Is there any DML statement which gets executed?? May be there is some trigger which runs on that DML causing the limit issue...

martinjhmartinjh

Not that I'm aware of and I certainly can't see any in the source code.

 

Basically all this should be doing is querying the database for a list of customers and presenting them in a dropdown list for selection - so it's nothing crazy or weird.

 

Plus this was working yesterday (or at least we few days ago) and the code has absolutely definitely not changed - because I would be the only one to make any changes.

 

Do you know if there have been any releases of SFDC code or changes to APIs that could be contributing??

Alex.AcostaAlex.Acosta

I don't know if this is possible, but could your initial query prior to the grouping function that Salesforce does for you be bringing back more than 50k record?

 

Can you confirm you have over 50k case records?

 

Lastly can you expand on your where clause to see if you can restrict the amount of records being brought back before the grouping to confirm if you're still getting this error?

martinjhmartinjh

Thanks for your reply Alex.

 

Yes this could concievably be the case, however I'm also pretty new to SFDC coding so could you tell me where/how I can run that SOQL to actually see how many Case records I have. Is that possible anywhere??

 

As for restricting the SOQL further, yes that could also be a possibility but I'd really like to confirm that will work before making any changes.

Alex.AcostaAlex.Acosta

There are a number of ways to do this.

 

system.debug([select count() from case]);
  • You could run a report
  • You can run in within the Salesforce IDE plugin within Eclipse inside the salesforce.schema broswer
martinjhmartinjh

I just ran the following in the SOQL Query tool in the Workbench:

 

SELECT count() FROM Case

 

And it returned: 50656 records - so this is more than likely the problem!

 

And I've identified a way to alter the SOQL to potentially restrict the results to more like 23k, so my next task is to figure out how/where to alter the Apex code in Sandbox and then migrate it over to Prod.

 

Many thanks for all your help!