Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
for(PricebookEntry pbe:[Select product2Id from PricebookEntry where PriceBook2Id = :pbId]){

I am getting the following exception.

System.LimitException: Too many query rows: 50001 An unexpected error has occurred. Your solution provider has been notified. (Database).

It should process the records in batch why am i getting the above error?

share|improve this question
up vote 1 down vote accepted

Even if you are executing in batch please note that the execute class still holds all the governor limits .Only the Object which is queried in Database.QueryLocator gets chunked while processing .The query in constructor or the start method or your scope is prevented from query limit .If you have 50000 pricebook entry for PriceBook then query will fail.

share|improve this answer
I want to query all the products from a pricebook and show it on VF page with pagination.Can you tell me any better way to query this. – Keepcalmncode Dec 27 '12 at 8:54
Vipul try using standard set controller using database.query locator .This will have pagination feature.… – Mohith Shrivastava Dec 27 '12 at 8:59
con = new apexPages.StandardSetController(Database.getQueryLocator([select Id , Name Product2 where Name like :('%'+SearchKeyword+'%')]); I am using the above query but I want to query the Products based on pricebook I have more than 50000 records. – Keepcalmncode Dec 27 '12 at 9:38
Vipul A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.So i would suggest set pagesize and limit to 1000 records on a page – Mohith Shrivastava Dec 27 '12 at 9:44
You can use filters also for your query – Mohith Shrivastava Dec 27 '12 at 9:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.