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
SharathKumar ReddySharathKumar Reddy 

How to compare multi-picklist field with picklist in apex query?

contact con = [select id from contact where Account.Multipicklist__c Includes (picklist__c)];
ANUTEJANUTEJ (Salesforce Developers) 
Hi Sharath,

>> https://salesforce.stackexchange.com/questions/204632/compare-multi-picklist-field-values-between-two-different-object

As mentioned in the above link, if you are trying to compare multi picklist values from two objects you can use the snippet in the link, for quick reference I am adding the best answer below:
 
Assuming that you can fetch records of both objects, and match up records that you want to compare...

This is a job for String.split() and Set.containsAll().

A multiselect picklist is basically just a string where the individual values that are selected are separated by a semicolon ;. Knowing that, String.split() can be used to obtain the individual selected values.

String myMSPicklist = 'test;abc;123';
// String.split() takes a regular expression.
// Passing a single delimiter (as long as it's not a special regex character 
//   like '.', '\', '[', etc...) works just fine
// String.split() gives you a list in return
List<String> individualSelections = myMSPicklist.split(';');

system.debug(individualSelections); // Should print ['test', 'abc', '123']
Yes, that gives you a List, and not a Set. Luckily, the Set class has a constructor that takes a List.

Given two sets, you can test them for equality using Set.containsAll(). As the name suggests, the method checks to see if the set that you call containsAll() on contains all of the elements in the set you pass into the method.

Set<String> testSet1 = new Set<String> {'1', '2', '3'};
// Sets are an 'unordered' collection, so it doesn't matter which order the 
//   elements of the set are placed into it.
Set<String> testSet2 = new Set<String> {'3', '2'};

system.debug(testSet1.containsAll(testSet2)); // Will print 'true' because both
//   elements of testSet2 ('3' and '2') exist in testSet1
system.debug(testSet2.containsAll(testSet1)); // Will print 'false' because testSet1
//   contains an element ('1') that testSet2 does not.
To determine equality of two sets, it's important to test both that the first set contains all elements of the second, and that the second set contains all elements of the first. As the example above illustrates, checking only one of the two can give you a false positive (but never a false negative).

That said, the easier route would be to just use Set.equals() (but it's nice to know how things work before using them).

The advantage to this approach is that it doesn't matter what order your values happen to be stored in (in your multiselect picklists that is). If the same values were selected, this method will find them.

One disadvantage is that this approach is case-sensitive. If you want to make this approach case-insensitive, you'd need to use String.toLowerCase() before you split it.
Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
SharathKumar ReddySharathKumar Reddy

Hi Anutez,

I need to compare multipicklist and picklist vlaues in filter condition
For Example: Multipicklist__c  contains valuee are A,B,C
Picklist contains value is B then only i will fetch records,
Example 2: Multipicklist__c  contains valuee are A,C
Picklist contains value is B then i will not able to fetch records,
Note: Whatever the value is there in picklist, same value should be there in multipicklist then only condition true