How do I build a SOQL query for records in a list of IDs?

  • Can some one 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'
    
  • Adam

    Adam Correct answer

    8 years ago

    If it's not in Apex code you don't need the : and you need to add ( )

    So your SOQL 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<Id> records = new Set<Id>{ 'a4fa0000000KzbV','a4fa0000000KzbW' }; //or an existing collection from elsewhere
    List<ContentVersion> results = [SELECT Id, Syndication_Documents__c 
           FROM ContentVersion 
           WHERE Syndication_Documents__c IN :records];
    

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM