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.
5 answers
You're close
select id from lead where id in ('00Q3000000zLxkFEAS', '00Q3000000eODvUEAW')
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:
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.List<Id> ids = ...;
List<SObject> results = [SELECT Id FROM Lead WHERE Id IN :ids];