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
Arun BalaArun Bala 

Multi pick list challenge ...

Folks,

Looks like we can only bind a String variable to a multi select column.

 

I have a situation where I need to pass a set of string to the SOQL:

for e.g: Select a.Id,a.OwnerId from Account a
where a.Lead_Rating__c includes ('B','C') 

 

Now when I try to construct  the parameter and pass it dynamically, say for e.g:

String a = '\'B\'';
String b = '\'C\'';
leadRatingStr = a+','+b;

 

and do :

Select a.Id,a.OwnerId from Account a
where a.Lead_Rating__c includes (:leadRatingStr) 

 

It doesnt work ... returns 0 rows where as the 1st query returned rows as expected. !! I simply dont understand. Any clue folks ?

 

 

 

Thanks

Arun BalaArun Bala
Any hint folks ? I appreciate your thoughts !!
micwamicwa

Two possiblities:

 

List<Account> a = [Select name from Account where name in ('test', 's')];

 

 or

 

List<String> myList = new List<String>(); . . List<Account> a = [Select name from Account where name in :myList];

 

 

 

 

hisrinuhisrinu

You have to add paranthesis as follows.

leadRatingStr = '('+a+','+b+')'; 

 

and try this.

 

Select a.Id,a.OwnerId from Account a where a.Lead_Rating__c in :leadRatingStr; 

JimRaeJimRae

You could also do a dynamic query, and build the query string from a loop.  This might need a little tweaking, but should get you started.

 

 

String querystr = 'Select a.Id,a.OwnerId from Account a where '; string querywhere=''; if(mymultiListstr.size==1){ querywhere = 'a.Lead_Rating__c = \''+x+'\''; }else{ for(String x : mymultiListstr){ querywhere = querywhere + 'a.Lead_Rating__c = \''+x+'\ or '; } querystr=querystr+querywhere.substring(1);//substring gets leading null off where clause Account[] accts = Database.query(querystr);

 

 

 

Arun BalaArun Bala

Srini,micwa - thanks for your responses. Actually we can only use includes() syntax( and not IN) when we are binding a variable to a multi pick list. Sorry if I was not clear on this part of the problem.

 

Jim - thanks & yes so far I see dynamic SOQL (building a query as a string and then using Database.query) as the only solution to this problem. 

 

 

Thanks again folks.

Message Edited by Arun Bala on 02-24-2009 10:49 AM