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?

1 Answer 1


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.

  • 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. Dec 27, 2012 at 8:54
  • Vipul try using standard set controller using database.query locator .This will have pagination feature. salesforce.com/us/developer/docs/pages/Content/… Dec 27, 2012 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. Dec 27, 2012 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 Dec 27, 2012 at 9:44
  • You can use filters also for your query Dec 27, 2012 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .