2

Hello I'm trying to use string in SOQL query where clause for multiselect picklist.

I need to get records of CustObj__c where Multipicklist__c fild have 'abc' or 'xyz' values.

String str = '\'abc\',\'xyz\'';
for(CustObj__c cust : [Select Id, Name, Multipicklist__c FROM CustObj__c WHERE Multipicklist INCLUDES (:str)]{
somelogic
}

However I get no records. What is wrong in this query?

Ok I tried using list too, here I'm getting all record of all Public Groups where user is assign. and saving those Public Groups names in

List<String> groupIds = new List<String>();
List<String> groupNames = new List<String>();

for(GroupMember gm :[SELECT Id,GroupId,UserOrGroupId FROM GroupMember WHERE UserOrGroupId = :userIds[0]]){
            groupIds.add(gm.GroupId);
        }
        for(Group g :[SELECT ID,Name FROM Group WHERE Id IN :groupIds]){
            groupNames.add('\'' + g.Name + '\'');
        }

for(CustObj__c cust : [Select Id, Name, Multipicklist__c FROM CustObj__c WHERE Multipicklist INCLUDES (:groupNames)]){
somelogic
}

It still not working

I get error in Dev console : Invalid bind expression type of List for column of type String

5
  • 3
    Pretty sure you would need to do INCLUDES ('abc', 'xyz'). Or declare a List<String> selections = new List<String> { 'abc', 'xyz' }; then INCLUDES :selections, but I don't have any multi-select picklists to test against.
    – Adrian Larson
    Commented Apr 8, 2020 at 19:48
  • Why are you adding '\' in the line groupNames.add('\'' + g.Name + '\'');? Also, you're doing a query in a loop. Please consider taking that out. Commented Apr 8, 2020 at 20:36
  • as my List<String> groupNames = {abc , xyz} and i thought it supposed to be like {'abc' , 'xyz'} so i modified it to groupNames.add('\'' + g.Name + '\''); output for List<String> groupName = {'abc' , 'xyz'}. In any case when i use this List groupNames in where clause it gives "Invalid bind expression type of List for column of type String" error
    – Lali
    Commented Apr 8, 2020 at 23:39
  • 1
    You cannot use bind variables with INCLUDES Commented Apr 9, 2020 at 3:13
  • The converse is EXCLUDES ('123') for the negative case where you want all but a few values. developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/… Commented Oct 18, 2023 at 17:39

4 Answers 4

2

Got my answer from this question thanks to @Anmol Kumar.

https://salesforce.stackexchange.com/a/301892/60388

Answer is below :

You need to create a query string first and then pass it to Database.query

Example

for(Group g :[SELECT ID,Name FROM Group WHERE Id IN :groupIds]) {
    groupNames.add(g.Name);
}

String value = '(\'' + String.join(groupNames, '\',\'') + '\')';
String queryString = 'SELECT Id, Name, MultiSelectPicklist__c FROM Program__c where MultiSelectPicklist__c INCLUDES ' + value;
List<program__c> newlist = DataBase.query(queryString);
system.debug('List of records: '+newlist);
0

Multi-select picklists requires to have a List to filter your INCLUDES against if you have multiple values to filter out:

List<String> strList = new List<String> { 'abc', 'xyz' };
for (CustObj__c cust : [Select Id, Name, Multipicklist__c FROM CustObj__c WHERE Multipicklist INCLUDES :strList) {
    ....
1
  • 1
    @it's not working. INCLUDE operator is not taking (:<List>string)
    – Lali
    Commented Apr 9, 2020 at 1:39
0

This is for you if you have XYZ, ABC, PQR picklist values in your multi-picklist field and wanted to query on it String sampleString = 'ABC';

Select id, Name, APINameOfMultiPickField FROM ObjectAPIName
WHERE APINameOfMultiPickField includes (:sampleString)
1
  • 1
    Welcome to Salesforce Stack Exchange (SFSE). Your answer needs improvement. Firstly, it is not valid Apex. Secondly, according to the comment (with a link to Salesforce documentation) from @DavidCheng under the question, you cannot use a bind variable (e.g., :sampleString) with INCLUDES. If you have information that this has changed, please include documentation. Please edit your answer to address these items.
    – Moonpie
    Commented Oct 18, 2021 at 13:11
0

The correct, working syntax is - Select id, Name, APINameOfMultiPickField FROM ObjectAPIName WHERE APINameOfMultiPickField includes ('sampleString')

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .