function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arek S.Arek S. 

Format of SOQL Query using IN clause

I'm sending a query to SalesForce (using the SOAP API) that includes an IN clause, however, I keep getting a MALFORMED_QUERY error.  Could someone point me in the right direction of what the query syntax is when using the IN clause?  I've tried the following without success (the ids are made up in these examples):

 

SELECT Id FROM Lead WHERE Id IN {'000000000000000','111111111111111'}

SELECT Id FROM Lead WHERE Id IN '0000000000000','111111111111111'

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You're close 

 

select id from lead where id in ('00Q3000000zLxkFEAS', '00Q3000000eODvUEAW')

All Answers

SuperfellSuperfell

You're close 

 

select id from lead where id in ('00Q3000000zLxkFEAS', '00Q3000000eODvUEAW')

This was selected as the best answer
Olivia Porter 1Olivia Porter 1
could you explain what the IN is doing?
kshannonkshannon
@Olivia Porter 1
IN allows you to specify a comma separated list in the WHERE clause to match across multiple matches. Instead of WHERE Name = 'Olivia' you could check for WHERE Name IN ('Olivia','Kyle','John') and it will give results for all 3.
Vinod VishwakarmaVinod Vishwakarma
@superfell thanks. Its working on my side.
Phil WPhil W
I think it worth adding to this question an important point. When you are performing a query from Apex you should use a bound variable instead of the comma separated sequence of Id values. The example in the original post becomes:
 
List<Id> ids = ...;

List<SObject> results = [SELECT Id FROM Lead WHERE Id IN :ids];

The major benefit of this approach, quite apart from the fact that it avoids the need to generate the "('id1', 'id2', ...)" text, is that the binding automatically avoids SOQL injection issues. If you take values from user input and that user knows how that value might be used, they could include some SOQL of their own by inserting a single quote in the value. When you then build the string, their single quote would be parsed as the end of the string and their text following that quote becomes some SOQL. I accept this isn't so big an issue here, since you are looking at Id strings and these don't include quotes - but you should get in the habit and stick with it.