use string array in IN clause in dynamic soql
This post by @ca_peterson explains you can directly use bind variables in dynamic soql
Alternatively, you can use this utility method to convert a Set into a String, which can be used in Dynamic SOQL.
//convert a Set<String> into a quoted, comma separated String literal for inclusion in a dynamic SOQL Query
private String quoteKeySet(Set<String> mapKeySet)
{
String newSetStr = '' ;
for(String str : mapKeySet)
newSetStr += '\'' + str + '\',';
newSetStr = newSetStr.lastIndexOf(',') > 0 ? '(' + newSetStr.substring(0,newSetStr.lastIndexOf(',')) + ')' : newSetStr ;
System.debug('quoteKeySet() : newSetStr ============ ' + newSetStr);
return newSetStr;
}
The easiest way is to use the array is as given below.
List<String> operators = new List<String> {'AND','OR'};
soqlString = 'SELECT Name FROM ACCOUNT WHERE Name != null';
soqlString += ' AND Operator__c in :operators';
Apex #automagically converts the reference to collection variables in string query to the corresponding sequence of strings inside the SOQL query.
Just another magic!