Before we get started, Multi-Select Picklists are one of Salesforce Administrators favorite things to hate:
- The Evils of Multi-Select Picklists in Salesforce
- A Big Pitfall in Salesforce Field Type: Multi-Select Picklist
And my personal favorite, a meme made famous by Steve Mollis:
With all that said, Jeffrey Berger knew the perils of what he was about to ask, but he asked on Twitter anyways seeking solution to compare selections between two multi-select picklist fields.
Multi-Select picklist fields store their values as semi-colon (;) delimited text, which is not an easy thing to parse in Flow without a little help from Apex. And even then, once parsed to know which individual values were selected there’s the challenge of comparing what was selected in the first picklist vs. the second picklist.
We can easily parse and compare the two picklist values in Apex, but that doesn’t necessarily mean our entire solution must reside in Apex. I’m a big proponent of using as little Apex as responsibly necessary to solve a problem.
At the time Jeff asked his question I was not clear on his specific use case, whether he needed to query for matching records whose picklist field contained at least one value that another record had or if he had two known records from Process Builder or Flow that he needed to compare.
I provided him advice on either approach which I go into a bit of detail here.
Find Matching Records via SOQL
Suppose you’re given scenario that you have a candidate applying for a job and you have a multi-select picklist on both objects, you want for any given candidate’s selected skills that you find all matching jobs needing at least one of those skills.
This requires querying Salesforce and best suited in Apex because SOQL has special functions for filtering by multi-select picklists:
I can find all jobs that require either the skill ‘Apex’ or ‘Visualforce’ using the INCLUDES operator:
SELECT id, name FROM Job__c WHERE skills__c INCLUDES( 'Apex;Visualforce' )
Compare Two Records via Flow & Invocable Apex
If you’re not searching for records based on multi-select picklist values but rather comparing the selected values between two records, then I developed a simple Invocable Apex plugin for Flow that identifies the common and unique values between the two picklist field values.
The purpose of this design was to fill a niche need that would be too complex or unreasonable to perform declaratively in Flow but without requiring the entire solution become an Apex trigger. Instead, by providing an Invocable Apex method then any Flow could use it as needed and this small piece of Apex now fits within larger declarative solutions.