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
chimerachimera 

Combination for multi select picklist using SOQL

From a visual force page, I return array of strings str[], which contains all the choices selected by the user through selectList with multi-"true". I have a multi-select picklist field in the object. I need to write a query that will return all the records from the object which matches wide variety of criteria. Here is an example:

 

There are five records in the object with values in the multi-select picklist as shown:

 

Type

Car

Car, Van

Car, Van, Jeep

Jeep

Jeep, Car, Bike

 

User selection in the str[] is {Car, Van, Jeep, Bike}

 

I want a SOQL query with filter on Type field which will return all the recrods from that object.

 

[select Name from someObject where Type includes (??)];

 

I would be very grateful if somebody can come up with solution.

 

Right now with my query all I am getting is records which have only single value picked.

Best Answer chosen by Admin (Salesforce Developers) 
Alok_NagarroAlok_Nagarro

Hi,

 

You can make run time query, plz refer code given below :

 

Suppose you have list having values from multiselect picklist named myValues.

List<String> myValues=new List<String>{'Car','Van','Jeep','Bike'};

String values='';

for(String s:myValues){

values+='\''+s+'\''+',';

}

String query='select name from someobject where Type includes ('+values.subString(0,values.length()-1)+')';

Database.query(query);

All Answers

Alok_NagarroAlok_Nagarro

Hi,

 

You can make run time query, plz refer code given below :

 

Suppose you have list having values from multiselect picklist named myValues.

List<String> myValues=new List<String>{'Car','Van','Jeep','Bike'};

String values='';

for(String s:myValues){

values+='\''+s+'\''+',';

}

String query='select name from someobject where Type includes ('+values.subString(0,values.length()-1)+')';

Database.query(query);

This was selected as the best answer
chimerachimera

Thank you Alok. My mistake was I was concatinating them without surrounding individual item in the list within single quotes.