Dependent Picklist is required for most of the time in our project. This post is showing how we can create dependent list in Lightning Web Component.
In this post, I have taken example of object and fields available in Salesforce Org. Object will be master picklist and field will be child which is populated based on object selected.
Below steps are required to created dependent list.
- Apex class to get object and fields
- Create Master and Child Picklist using lightning-combobox
1. Apex class to get object and fields:
To get Salesforce Org’s objects we can use Schema.getGlobalDescribe(). We can set filter, if we need only custom object, history object etc.
To get fields also we can utilize Schema.getGlobalDescribe() with specific object. sObject.getDescribe().fields will get list of all fields available for object. Iterator is used to loop all available fields.
Apex Code
2. Create Master and Child Picklist using lightning-combobox
As we have apex code now for getting object and fields. Let us create pick lists in Lighting Web Component. I have used Wire to get data from apex call and WireMethod to format result for lightning-combobox data.
Full Code of LWC
Reference: