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
Monishan MMonishan M 

Too many query rows: 50001 error

Hi,

I am trying to fetch Accounts with First Name contains "SA" but I am receiving the below error 
Too many query rows: 50001
Error is in expression '{!doSearch}' in component <apex:commandButton> in page searchaccts: Class.GASUtilities.getMatchingAccountIds: line 65, column 1
Class.GASUtilities.getAccountIdsToExclude: line 30, column 1
Class.searchAccts.excludeAccountsIfAny: line 969, column 1
Class.searchAccts.doSearch: line 528, column 1

Could you please guide me what should be done to eliminate this error?


Below is my Apex  code piece as per the above error lines

Class.searchAccts.excludeAccountsIfAny: line 969, column 1

Set<ID> idsToIncludeExclude = GASUtilities.getAccountIdsToExclude(UserInfo.getUserId(), accountIds); 
        //mnaidu - 1/9/2014 - check to see if the filters are supposed to be retain the filter results
        // or exclude the filter results from the general unfiltered search
        Boolean retain = (GAS_Hierarchical_Setting__c.getInstance().Enable_CB_Filter_Retain_Mode__c == null) 
                                    ? false : GAS_Hierarchical_Setting__c.getInstance().Enable_CB_Filter_Retain_Mode__c ;
        if(retain && (idsToIncludeExclude == null || idsToIncludeExclude.size() == 0)){
            //no accounts should be shown
            accountIds.clear();
            return accountIds;
        }
        if(idsToIncludeExclude!=null && idsToIncludeExclude.size() > 0){
            originalAcctIds.addAll(accountIds);
            if(retain){
                originalAcctIds.retainAll(idsToIncludeExclude);
            }
            else{
                originalAcctIds.removeAll(idsToIncludeExclude);
            }
            
            accountIds.clear();
            accountIds.addAll(originalAcctIds);
        }
        return accountIds;
    }


Class.searchAccts.doSearch: line 528, column 1  if(GAS_Hierarchical_Setting__c.getInstance().Do_Criteria_Based_Filtering__c){
                acctIds = excludeAccountsIfAny(acctIds);
            }
           // Error if nothing found
            if (acctIds.size() == 0) {
                ApexPages.addMessage (new ApexPages.Message(ApexPages.Severity.WARNING, GAS_NOMATCH));
                return null;
            }

Thanks
RituSharmaRituSharma
As per APEX governor limits, your SOQL queries in one context/execution can't return more than 50000 records. In your query, put filters and limit clause to fetch only 50000 records.

https://developer.salesforce.com/docs/atlas.en-us.salesforce_app_limits_cheatsheet.meta/salesforce_app_limits_cheatsheet/salesforce_app_limits_platform_apexgov.htm
SwethaSwetha (Salesforce Developers) 
HI ,
To fix this, You will need to use Batch Apex, in which the 50k limit counts per batch execution

Usig the  Limits.getQueryRows() and Limits.getLimitQueryRows() will help you calculate the number of rows in the transaction and optimize your code.

The below links will give an idea of the approaches you can follow

> Question about "System.LimitException: Too many query rows: 50001"
https://developer.salesforce.com/forums/?id=906F0000000925uIAA

> LimitException: Too many query rows: 50001 from with count() aggregate function
http://stackoverflow.com/questions/9984349/limitexception-too-many-query-rows-50001-from-with-count-aggregate-function

>System.LimitException: Too many query rows: 50001
https://developer.salesforce.com/forums/?id=906F000000091sVIAQ

>Question about "System.LimitException: Too many query rows: 50001"
https://developer.salesforce.com/forums/?id=906F0000000925uIAA

>>Too Many Query Rows 50001
https://developer.salesforce.com/forums/?id=906F00000008z3SIAQ

>>System.LimitException: Too many query rows: 50001
http://salesforce.stackexchange.com/questions/28228/system-limitexception-too-many-query-rows-50001

>Too many query rows 50001
http://salesforce.stackexchange.com/questions/40916/too-many-query-rows-50001

>System.LimitException: Too many query rows: 50001 for Test Class
http://salesforce.stackexchange.com/questions/69954/system-limitexception-too-many-query-rows-50001-for-test-class

>LimitException: Too many query rows: 50001 from with count() aggregate function
http://stackoverflow.com/questions/9984349/limitexception-too-many-query-rows-50001-from-with-count-aggregate-function

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you