How to use controlling and dependent picklist in Salesforce Lightning
Hi guys, let’s learn today how to use picklist in Salesforce with a real scenario.
Business Problem
One of my client is facing an issue, currently there is a Disposition component which is used by all Sales users. This component contains three dropdown menus
- Status (controlling field),
- Action 1 (dependent on Status), and
- Action 2 (dependent on Action 1) picklist
On updating the values in each picklist requires modifying multiple, unnecessary values in the custom metadata types — Disposition Status, Disposition Action 1, and Disposition Action 2
Let’s look deeper into the problem
Here Disposition component is a Lightning component used on Lead/Opportunity detail page in LEX
This component is using 3 Custom Metadata Objects and 3 custom picklist fields on Lead object
The three custom metadata object -
- Status__mdt
- Action_1__mdt
- Action_2__mdt
This is used to display records with their relationship on Lead detail page
The three custom picklist fields on Lead object -
- Status__c (Controlling)
- Action_1__c (Dependent on Status__c)
- Action_2__c (Dependent to Action_1__c)
These custom fields on lead store the selected picklist values from metadata on Lead record.
Let’s take an example to understand how it is working currently -
Let say I have a lead “AW Consulting” with following picklist values
Here Status is controlling picklist value, Action 1 is dependent on Status and Action 2 is dependent on Action 2
Analyse : Let’s find the PAIN points
Using above example, If I have to link Contact to AW Rescheduled Appointment, here AW Rescheduled Appointment is Disposition_Status. For this I will need to do following:
- First of all create Disposition_Action_1 record with Action_1_Status__c = Contact and link Disposition_Status__c to AW Rescheduled Appointment
- Create new Disposition_Action__2 record with Disposition_Action_1__c = Contact, link Disposition_Status__c to AW Rescheduled Appointment
Do you feel the pain of updating at so many places?
Let’s Summarize
- Difficult to maintain picklist values
- Currently I cannot reuse same Action 1 for other Status due to which there are a lot of duplicate values which is very hard to manage
- There is no NEW button on parent related list so I have to remember parent-child relationship to update values
- If I have to add/update a picklist value then I need to update it at multiple places
Boom! Let’s solve this, time to think for a while of the best possible solution
.
.
.
.
Let’s go
Solution
To solve this problem, I used the 3 custom picklist fields on Lead object. Remember the 3 custom fields I told you above?
- Status__c (Controlling)
- Action_1__c (Dependent on Status__c)
- Action_2__c (Dependent to Action_1__c)
I added the above fields in Lead page layout
Create apex class to fetch the above picklist values and display in custom Disposition Component.
The Code
By leveraging the picklist relationships provided by Salesforce and apex, this problem can be easily solved. Let’s take a look at the code