SOQL: Populate a Literal List in WHERE IN Clause

You can supply string variable values using Apex binding. When you're not using Dynamic SOQL, as you're not here, you can even use complex Apex expressions in the bind. The following options are all legit.

... WHERE Name IN (:STRING_ONE, :stringTwo)

... WHERE Name IN :new List<String>{STRING_ONE, stringTwo}

... WHERE Name = :STRING_ONE OR Name = :stringTwo

List<String> names = new List<String>{STRING_ONE, stringTwo};
[SELECT ... FROM Account WHERE Name IN :names]

Dynamic SOQL doesn't allow complex bind expressions, so creating new Lists and similar are verboten in that context.


It's like normal SOQL, but you put the colons before each item in the list:

public class MyClass {
    private final String STRING_ONE = 'STRING ONE';

    public MyClass(String stringTwo) {
        List<Object__c> objects = [
           SELECT Id
           FROM Object__c
           WHERE Name IN (:STRING_ONE, :stringTwo)
        ];
    }
}

Tags:

List

Where

Soql