How do I build a SOQL in a list of IDs?

381    Asked by DavidEDWARDS in Salesforce , Asked on May 11, 2023

Can someone correct my soql? I need to export records in data loader like:

WHERE Id IN ('123123123','123123fsdf')
But this following query does not work for me:
SELECT Id, Syndication_Documents__c FROM ContentVersion 
WHERE Syndication_Documents__c in : 'a4fa0000000KzbV','a4fa0000000KzbW'
Answered by Clare Matthews
If it's not in Apex code you don't need the : and you need to add ( )

So your SOQL in list could be rewritten as:
SELECT Id, Syndication_Documents__c
   FROM ContentVersion
   WHERE Syndication_Documents__c IN ('a4fa0000000KzbV','a4fa0000000KzbW')
If you're doing it from Apex and have a Collection (Set, List, or Map) you can bind them to the query and avoid the string manipulation:
Set records = new Set{ 'a4fa0000000KzbV','a4fa0000000KzbW' }; //or an existing collection from elsewhere
List results = [SELECT Id, Syndication_Documents__c
       FROM ContentVersion
       WHERE Syndication_Documents__c IN :records];

Your Answer

Interviews

Parent Categories