Specify large set of IDs in SOQL query

Edit: The new SOQL limit is 100,000 characters, but you only get 4,000 characters for the where clause in the new limits. You'll definitely want to use the retrieve call (now available in REST) to get up to 2,000 records at a time.


The raw SOQL limit is 20,000 characters, so you can't make a raw dynamic statement that exceeds this value. You'll need to chunk your query into multiple parts. In theory, you'll get around 1,000 rows, maximum, depending on how many fields you use (and assuming you 15-character length ID values). If possible, consider using the retrieve call, which only works by ID value, but allows 2,000 rows at once to be retrieved (larger than you could do by raw SOQL). You'll have quite a bit of API calls using this method, since it's not limited by the 20,000 character limit.


You can certainly chuck them in the SOQL as a set or use the OR operator:

SELECT Id FROM Account WHERE Id IN ('00100000001abcX', '00100000001abcY')

OR

SELECT Id FROM Account WHERE Id = '00100000001abcX' OR Id = '00100000001abcY'

Although be careful as there is a limit of number of characters used in the SOQL string. I think it's something around the 3000 mark (you can find the exact number in the Salesforce Docs).

I'm afraid that you'll have to split the IDs and do this in batches, keeping the character limit under.

Update:

The limit is 20,000 as per @sfdcfox's answer!

Tags:

Soql

Bulk Api