Last Updated on April 26, 2022 by Rakesh Gupta
Big Idea or Enduring Question:
How to count the number of selections in a multi-select picklist?
After reading this blog, you’ll be able to:
- Count the number of selections in a multi-select picklist
- understand how the PICKLISTCOUNT formula works
- and much more
Business Use case
Anita Smith is working as a Salesforce Consultant at Gurukul On Cloud (GoC). She has received a requirement from her Sales director to allow users to select only three options in the Preferred Destination multi-select picklist on Lead.
Guided Practice (We-do):
If you ever worked with a multi-select picklist, you know the pain when it comes to writing any logic using it. One of the common ask is how do I count the number of selected values when working with a multi-select picklist.
Now you can use PICKLISTCOUNT() formula to count selected values for the multi-select picklist.
|PICKLISTCOUNT(multiselect_picklist_field)||It Returns the number of selected values in a multi-select picklist.|
There are 2 steps to solve Anita’s business requirement using the Validation Rule. We must:
- Create a custom multi-select picklist on lead to track preferred destinations for vacation
- Write a validation to display the error message, if more than three values have been selected for the destination
Step 1: Create a Custom Picklist(Multi-Select) Picklist on Lead
First of all, we will create a multi-select picklist field on lead to track preferred destinations for a vacation.
- Click Setup.
- In the Object Manager, type Lead and select Lead.
- Select Field & Relationship, then click New.
- Select Picklist (Multi-Select) as Data Type, then clicks Next.
- Choose output Type
- Enter Field Label and click the tab key, the API Name will populate.
- Select Enter values, with each value separated by a new line for Values.
- Copy the countries’ values from GitHub and paste them into the box.
- # Visible Lines 4.
- Click on the Next button.
- Set the Field-level Security for the profiles, make sure to set this field as read-write.
- Add this field to Page Layout.
- Click Save.
Step 2: Creating a Validation Rule
- Click Setup.
- In the User Interface, clicks on Object Manager
- Navigate to Lead | Validation Rules and click on the New button.
- Enter a name in the Rule Name.
- Active: True
- Error Condition Formula
- PICKLISTCOUNT( rakeshistomMVP__Preferred_Destination__c ) >3
- Error Message
- Error Message: You can only select a maximum of 3 countries.
- Error Location: Preferred Destination
- Click Save.
Proof of Concept
Next time, when a Sales rep creates or updates a lead with more than three values in Preferred Destination – it will display an error message defined in the Validation Rule.
I want to hear from you!
What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.