+ Start a Discussion
jvolkovjvolkov 

Querying Multi-Select Picklists

Is it possible to query Multi-Select Picklists to lookup against a Set?  

 

For example:


Instead of this 

SELECT Id, MultiPicklist__c from CustObj__c WHERE MultiPicklist__c includes ('AAA;BBB','CCC')

Can you do something like this?

I get an error when attempting to save this so I'm wondering what the correct syntax, if possible, for something like this would be.

Set<String> singlePicklistSet = new Set<String>();  /*the set of values from a single picklist*/
SELECT Id, MultiPicklist__c from CustObj__c WHERE MultiPicklist__c includes :singlePicklistSet  /*is at least one of the values from MultiPicklist__c in the set of values from the single picklist?*/
Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

In my experience, you have to split the set into a delimited string, and then query against that string

 

 

String slist = '';
for (String s: singlePickListSet) {
slist += '\'' + s + '\',';
}
slist = slist.substring (0,slist.length() -1);

SELECT Id, MultiPicklist__c from CustObj__c WHERE MultiPicklist__c includes :slist

Or you can use dynamic SOQL:

String squery = 'SELECT Id, MultiPicklist__c from CustObj__c ';
squery += ' AND MultiPicklist__c INCLUDES (' + slist + ')';

CustObj__c [] mvptest = database.query(squery);

 

 

All Answers

cloudcodercloudcoder

I don't exactly follow what your use case is trying to achieve, but check out the following blog entry which I think should help:

 

http://blog.sforce.com/sforce/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html

BritishBoyinDCBritishBoyinDC

In my experience, you have to split the set into a delimited string, and then query against that string

 

 

String slist = '';
for (String s: singlePickListSet) {
slist += '\'' + s + '\',';
}
slist = slist.substring (0,slist.length() -1);

SELECT Id, MultiPicklist__c from CustObj__c WHERE MultiPicklist__c includes :slist

Or you can use dynamic SOQL:

String squery = 'SELECT Id, MultiPicklist__c from CustObj__c ';
squery += ' AND MultiPicklist__c INCLUDES (' + slist + ')';

CustObj__c [] mvptest = database.query(squery);

 

 

This was selected as the best answer
forecast_is_cloudyforecast_is_cloudy

jvolkovjvolkov

Awesome that worked, only I changed

 

String squery = 'SELECT Id, MultiPicklist__c from CustObj__c ';
squery += ' AND MultiPicklist__c INCLUDES (' + slist + ')';

to

String squery = 'SELECT Id, MultiPicklist__c from CustObj__c AND MultiPicklist__c INCLUDES (' + slist + ')';

 

The former was giving an error of unexpected token: AND

 

Thanks for your help on this!