0

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

  • 1
    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 Apr 8 '20 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. – Drew Kennedy Apr 8 '20 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 – Ghosty Apr 8 '20 at 23:39
  • 1
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) {
    ....
  • @it's not working. INCLUDE operator is not taking (:<List>string) – Ghosty Apr 9 '20 at 1:39
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.