You need to sign in to do that
Don't have an account?
loneboat
Documentation for SOQL "WHERE x IN :y" syntax?
Can someone point me to the documentation for SOQL's "WHERE x IN :y" syntax, where y is a collection type (list, set, map). I've tried searching for it, but "where" and "in" are such generic terms that my searches are essentially as useful as just searching for "soql" alone.
Thanks!
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_select_comparisonoperators.htm
and even more complete info
http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_variables.htm
The colon (":") is a binding statement. You can bind almost any legal Apex Code expression here, not just a variable name. Normally, you'd use one of the following:
Where "typed-field" matches the type of typedList, typedSet, typedMap.keyset(), or typedMap.values().
For example, if you are querying on AnnualRevenue, you can use List<Decimal>, Set<Decimal>, Map<Decimal,anyType>.keySet(), or Map<anyType,Decimal>.values().
The ID types work with List<SObject>, Set<Id>, Map<Id,Sobject>.keyset(), or Map<id,sobject>.values().
OK, but how does this work in dynamic SOQL ? What's the syntax for that, please ?
Rup
From what I understand, if you want to use this in dynamic SOQL, you have to build up the string explicitly:
My apologies for the late reply, but you can use dynamic bindings without generating your own CSV list:
The variable must be visible in the current scope for this to work.
Is variable binding in dynamic SOQL relatively new? I have used it plenty of times, but only recently. I remember having issues with it working, but I'm not sure if it wasn't supported before, or I just wasn't using it correctly.
I remember using it just shortly after dynamic apex came out, but it was unpredictable at first (but seems much better now).
For example, if I have 3 parent records which each have 3 child records, and I have a list of the parent Ids off the child records so there would be 3 duplicates of 3 ids, would the SOQL for the parent object such that Id IN ParentIdList return 3 or 9 rows?
Its working fine...
List<Id> acctIds = new List<Id>{'id-one', 'id-two', 'id-three', ... }; String SOQL_IDs = ''; for (Id id : acctIds) { String id_in_quotes = '\''+id+'\''; if (SOQL_IDs!='') { SOQL_IDs+=','; } // add a comma if this isn't the first one SOQL_IDs += id_in_quotes; } String SOQL = 'SELECT Id, Name from Account WHERE Id IN ('+SOQL_IDs+')'; List<Account> accounts = database.query(SOQL);
Here's a filter on some UK postcodes.
List<String> pcList = new List<String>{'MK%','LN%','IP%','CV%','KT%'};
String queryString = 'SELECT Id, Name FROM Account WHERE BillingPostalCode LIKE :pcList LIMIT 20';
List<Account> lstAccounts = database.Query(queryString);
System.debug(lstAccounts);
see https://salesforce.stackexchange.com/questions/37113/how-to-use-like-and-in-together-in-soql
and https://salesforce.stackexchange.com/questions/65950/soql-bulk-fuzzy-searching
List<String> pcList = new List<String>{'M%'};
List<String> pcNoList = new List<String>{'MK%'};
String queryString = 'SELECT Name, BillingPostalCode FROM Account WHERE (BillingPostalCode LIKE :pcList) AND (NOT BillingPostalCode LIKE :pcNoList) LIMIT 20';
List<Account> lstAccounts = database.Query(queryString);
System.debug(lstAccounts);