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
solarcookersolarcooker 

Understanding and resolve Too many query rows: 50001

Hello,

I'm beginner with apex and SOQL and I recently have problem with "Too many query rows: 50001" exception.
I thought this exception occurs when the SOQL query returns more than 50000 rows.
I have a class with 4 query but 2 of then cause the problem. The first query counts the number of products sold and group it by owner for each type of product: number of owner = 10, number of product = 10; total query = 10, total rows = 100
The second query counts the number of products sold and group it by month for each owner (center.name): number of month = 12, number of owner = 10; total query = 10, total rows = 120

NOTE: Total of number products sold is more than 50000. See bellow my requests:
//Get sales per stove type at each center
    	for (String stoveType : STOVE_TYPES) {
	    	AggregateResult[] centerNbSales = [SELECT owner.userRole.name salesCenterName, count(id) nbSales FROM Sale__c 
				WHERE Stove__r.Type__c LIKE :stoveType AND CALENDAR_YEAR(Date_of_Sale_real__c) =: System.today().Year() 
				GROUP BY owner.userRole.name];
			
			//Initialize the map with center name and sales = 0
			tmpCenterNbSalesMap = new Map<String, Integer>();
			for(String cName : centerName) tmpCenterNbSalesMap.put(cName, 0);
				
			for (AggregateResult ag : centerNbSales) tmpCenterNbSalesMap.put((String)ag.get('salesCenterName'), (Integer)ag.get('nbSales'));
			
			//Put the number sold stoves for each center for the current stove type
			salesStoveTypeMap.put(stoveType, tmpCenterNbSalesMap);
    	}
//Get the number of direct sales per month
AggregateResult[] tmpDateOfSalesList = [SELECT CALENDAR_MONTH(Date_of_Sale_Real__c) month, count(ID) nbSales FROM Sale__c
                                          WHERE CALENDAR_YEAR(Date_of_Sale_Real__c) = :year
                                          AND Stove__r.Selling_Center_3__c = :center.Name
                                          GROUP BY CALENDAR_MONTH(Date_of_Sale_Real__c)];
Please clarify me if I misunderstand the meaning of the "Too many query rows: 50001" exception and any help would be welcome.
(the class contains 700 lines but I can post it if necessary)

Thanks in advance.
Best Answer chosen by solarcooker
solarcookersolarcooker
Hello,

I found the reason of my problem in this post https://developer.salesforce.com/forums?id=906F000000090NlIAI and the solution which works in my case here https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_readonly_context_pagelevel.htm
I hope the links above will help others too.

Regards
 

All Answers

Sagar PareekSagar Pareek
You need to introduce more filter criterias in queries you are using.
solarcookersolarcooker
Thanks for your very quick answer.

What would you like to say about "more filter criterias" because my queries return exactly what I need. For instance the second query returns something like the figure below (number of total sale for each month at one center):
Query result exemple
And the results of the first query is similar, the difference is that the result is grouped by owner (center name) instead of by month.
Perhaps could you give me an example about the filter criterias you told before?

Regards
solarcookersolarcooker
Hello,

I found the reason of my problem in this post https://developer.salesforce.com/forums?id=906F000000090NlIAI and the solution which works in my case here https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_readonly_context_pagelevel.htm
I hope the links above will help others too.

Regards
 
This was selected as the best answer