+ Start a Discussion

How to construct a SOQL query for retrieving more than 30,000 records?

I am using the PHP SOAP library and am trying to construct a set of queries that will allow me to retrieve over 30,000 records. Since one query can only retrieve up to 2000 records, I am having a problem with setting the criteria to get me the whole batch of 30,000 records in 15 queries. A part of the problem is that I am using the SystemModstamp field as a part of the criteria, so that I don't get the same records over and over again. However, our marketing department uses a tool that mass inserts records (thousands at a time) so the modstamp is the same for several thousand records. That is making this query not retrieve all of the records, instead, it is retrieving many of the same.


The query I am using is:


SELECT Id, CampaignId, Status, ContactId, CreatedById, CreatedDate, FirstRespondedDate, HasResponded, IsDeleted, LastModifiedById, LastModifiedDate, LeadId, SystemModstamp FROM CampaignMember WHERE CampaignId = 'SPECIFIC_CAMPAIGN_ID HERE' AND SystemModstamp > INSERT_LAST_MODSTAMP_OF_LAST_RECORD_HERE ORDER BY SystemModstamp ASC LIMIT 2000

 Any recommendations on how I can go about doing this? I can use the Excel connector to retrieve records, so surely there is a way. I just can't figure out how I should construct the query.


Thanks much in advance!



Jake GmerekJake Gmerek


That should solve your issue.

This looks interesting though (Warning I have never used it, I just came across it and thought it looked interesting):



Use the criteria to get all 30K rows, and then use queryMore to fetch all the results, there's no need for you to manually chunk it up yourself.