Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save douglascayers/2067559d9591e7471b412e7b0343725f to your computer and use it in GitHub Desktop.

Select an option

Save douglascayers/2067559d9591e7471b412e7b0343725f to your computer and use it in GitHub Desktop.
Apex example of determining if two multi-select picklist fields contain similar values as asked by https://twitter.com/jeffereyberger/status/877214452453658628
// in apex, the values from multi-select
// picklists are simply delimited strings
String picklistA = 'Apple;Orange;Banana'; // obj1.picklist__c
String picklistB = 'Apple;Pear;Plum;Orange'; // obj2.picklist__c
// split the strings into individual values and store as unique sets
Set<String> picklistValuesA = new Set<String>( picklistA.split(';') );
Set<String> picklistValuesB = new Set<String>( picklistB.split(';') );
System.debug( 'picklistValuesA: ' + picklistValuesA );
System.debug( 'picklistValuesB: ' + picklistValuesB );
// determine which values belong in both sets
Set<String> commonValues = new Set<String>( picklistValuesA );
commonValues.retainAll( picklistValuesB );
System.debug( 'picklists have these common values: ' + commonValues ); // 'Apple', 'Orange'
// if the resulting set has any values in it then
// yes, the two picklists have that many similar values
Boolean haveAtLeastOneCommonValue = ( commonValues.size() > 0 );
System.debug( 'picklists have at least one common value? ' + haveAtLeastOneCommonValue );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment