0

I'm trying to get records of custom object where MultiSelectPicklist__c have names of publicgroups.

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

for(GroupMember gm :[SELECT Id,GroupId,UserOrGroupId FROM GroupMember WHERE UserOrGroupId = '0051U000006AkQy']){
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, MultiSelectPicklist__c FROM CustObj__c WHERE MultiSelectPicklist__c INCLUDES (:groupNames)] ){
somelogic
}

Why it says 'Invalid bind expression type of List<'String> for column of type String' in developer console?

Even after converting List to string I'm still not able to get any record

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 + '\'');
            //groupNames.add(g.Name);
        }

        String listtostring = String.join(groupnames, ',');

        List<program__c> newlist = [SELECT Id, Name, MultiSelectPicklist__c FROM Program__c where MultiSelectPicklist__c INCLUDES (:listtostring) ];
        system.debug('List of records---'+newlist);
  • I tried both
for(Group g :[SELECT ID,Name FROM Group WHERE Id IN :groupIds]){
            ///groupNames.add('\'' + g.Name + '\'');
            groupNames.add(g.Name);
        }
4
  • Please edit your post to specify the line that is throwing the error. Apr 9 '20 at 0:12
  • for(CustObj__c cust : [Select Id, Name, MultiSelectPicklist__c FROM CustObj__c WHERE MultiSelectPicklist__c INCLUDES (:groupNames)] ){ This is the line it shows error on my code..
    – Watson
    Apr 9 '20 at 0:18
  • 1
    please do not post duplicate questions. Apr 9 '20 at 1:50
  • No. I tried suggestion provided which is what my code show above. but it throws that error. Why i'm not able to put List<String> after INCLUDES ? I tried converting List<string> to string too, then It don't give error but there is no expected result.
    – Watson
    Apr 9 '20 at 1:52
1

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

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