Use Case: - The company called XYZ is using many ( 100+ ) lightning web components and out of 20+ LWC are using dependent pick-list. As a developer, you have been asked to develop a generic ( reusable ) component which can work for any objects & for any component.
Hi Folks,
Today I came up with a new blog for the reusable dependent picklist. In this blog, you guys will see how to create a reusable generic picklist without using apex so let’s get started.
Before we start development, let’s talk about the use case & Solution.
The company called XYZ is using many ( 100+ ) lightning web components and out of 20+ LWC are using dependent pick-list. As a developer, you have been asked to develop a generic ( reusable ) component which can work for any objects & for any component.
Solution: Create a web component which uses inbuilt JavaScript API to get the pick-list details and creates a generic dependent pick-list for any object. ( There are some limitations of the ui*API, check the supported object Here ).
Step 1 – Create a Lightning Web Component and name it GenericDependentPicklist
Step 2 – Test the Pick-list.
Create a web component and give it a name of your choice. For .html file use below code
<template>
<div class="">
<lightning-card variant="Narrow"
title="Generic Solutions" icon-name="standard:record">
<div class="slds-m-around_small" style="color: red;" >
Note:- Task Object is not supported by lightning/*Api
</div>
<div class="slds-m-around_small">
Account Object
<c-generic-dependent-picklist
object-api-name="Account"
dependent-picklist-api-name="CustomerPriority__c"
dependent-picklist-label="Customer Priority"
controlling-picklist-api-name="Industry"
controlling-picklist-label="Industry"
onselectedpicklists={handlePicklist} >
</c-generic-dependent-picklist>
<template if:true={selectedvalues}>
<div class="slds-m-around_small">
<p style="color: red;">
Selected Controlling Picklist : {selectedvalues.controlling}
</p>
<p style="color: red;">
Selected Dependent Picklist : {selectedvalues.dependent}
</p>
</div>
</template>
</div>
</lightning-card>
</div>
</template>
Code Comments: – In our test component, we are using our generic component.
<c-generic-dependent-picklist
object-api-name="Account"
dependent-picklist-api-name="CustomerPriority__c"
dependent-picklist-label="Customer Priority"
controlling-picklist-api-name="Industry"
controlling-picklist-label="Industry"
onselectedpicklists={handlePicklist} >
</c-generic-dependent-picklist>
object-api-name : – is used to get the Object API Name. So, make sure that you are passing the valid API name for the object.
dependent-picklist-api-name :- The API name for the dependent picklist field
dependent-picklist-label :- Field Label for the dependent pick-list field.
controlling-picklist-api-name :- API name for the controlling pick-list field
controlling-picklist-label : – Field Label for the Controlling pick-list field.
onselectedpicklists – Event handler which accepts the JS method in the calling component ( in our case it is test component ) to handle and get the selected values.
for .js file use below code between { }
selectedvalues;
handlePicklist(event) {
let selectedValues = event.detail.pickListValue;
window.console.log('\n **** selectedValues **** \n ', selectedValues);
this.selectedvalues = JSON.parse(JSON.stringify(selectedValues));
}
for meta XML file using below code
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Once you have created the test web component. Use this web component in either app page, record detail page or home page.
At Last I wanna thanks Amit for giving this platform to share with you guys, see you until next blog 🙂
This is awesome. Thanks Saket for sharing such wonderful blog
U said paste the code But we cant copy the code
Please check now.
hey . in parent component how can i get a button to create record based on these picklist.
You have to develop the button
Can we get the code for Multi-level dependant picklist, as this is two level. Thanks!
Unfortunately we do not have and you have to develop the same