So I have a query that is returned more than 50,000 results. Its just a query on contacts. But Im not sure how to limit the query and then continue getting the rest of the results. I was considering this.

  • Running the query with limit 50,000 ordered by Id.
  • If the results number less than 50,000, break and continue
  • If more, run the query again but where Id greater than the 50,000th one I got back in my previous run.

Would that work? ordering by Id and then doing greater than? If not can someone tell me how I manage this?

I know the easiest thing to do here would be test, but we dont have an org set up for this case, so someone has to go and set it up. So while Im waiting I figured Id ask to see if im on the right track.

  • The 50,000 limit is for all combined queries. If you need more, you'll need to use a specific technique depending on your use case.
    – sfdcfox
    May 29 '17 at 14:50
  1. If the purpose of querying these many records is to show on some page, then pagination is best approach to show the records in chunks. http://www.sfdcpoint.com/salesforce/pagination-using-standard-set-controller-salesforce/

  2. If you need some processing to be done on those records then batch class is the best way of doing it. The batch class can handle 50 million records processed in chunks of maximum 2000 records.

Please follow the below links for your reference:

http://blog.shivanathd.com/2013/01/how-to-write-batch-class-in.html?m=1 https://trailhead.salesforce.com/en/modules/asynchronous_apex/units/async_apex_batch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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