There are quite a few options to solve this problem.
First, there is an idea Apex Code: Implement Iterable<T>
on Set<T>
., which when implemented would eliminate the need for the below workarounds.
Example on Set<String>
Set<String> mySet = new Set<String>();
mySet.add('One');
mySet.add('Two');
mySet.add('Three');
System.debug('mySet= ' + mySet);
Debug:
08:53:25.1 (2682641)|USER_DEBUG|[5]|DEBUG|mySet= {One, Three, Two}
Option 1 - cast it to Iterable<T>
String mySet_Joined = String.join((Iterable<String>)mySet, ', ');
System.debug('mySet_Joined= ' + mySet_Joined);
Result:
08:53:25.1 (2944980)|USER_DEBUG|[8]|DEBUG|mySet_Joined= One, Two, Three
UPDATE re- Option 1 - see this post for more info about why (Iterable<String>)mySet
should not be used.
Option 2 - convert original Set into List
String mySet_Joined_List = String.join(new List<String>(mySet), ', ');
System.debug('mySet_Joined_List= ' + mySet_Joined_List);
Result:
08:57:13.1 (2812911)|USER_DEBUG|[11]|DEBUG|mySet_Joined_List= One, Two, Three
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);
Best Answer
Just join the values with a semi-colon:
Alternatively, you might consider binding the multi-select list directly to the field: