Home Salesforce Dependent PickList In Lightning Web Component

Dependent PickList In Lightning Web Component

by Dhanik Lal Sahni

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.

  1. Apex class to get object and fields
  2. 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:

  1. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_schema.htm
  2. https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex
  3. https://developer.salesforce.com/docs/component-library/bundle/lightning-combobox/example

You may also like

Leave a Comment