Hello #OhanaMembers,
I was working on a requirement where I had a requirement to implement the custom Multiselect combo box so that end users can select multiple options from the picklist.
We are not using the standard multi-select component as the length of the values is too big and the business users can not hover to each option to see which option they wanted to select.
So, As a Salesforce Architect, I have asked my team to built a reusable component and I am posting the same component here so that if anyone have such requirement in future can be get benefitted.
So, let’s start building the component. create a custom Lightning web component and use below code for JavaScript and HTML file.
How to use
component receives the following params:
label – String with label name;
disabled – Boolean value, enable or disable Input;
options – Array of objects [{label:’option label’, value: ‘option value’},{…},…];
to clear the value call clear() function from parent:
let multiSelectPicklist = this.template.querySelector('c-multi-select-pick-list');
if (multiSelectPicklist) {
multiSelectPicklist.clear();
}
to get the value to receive “valuechange” event in the parent;
the returned value is the array of strings – values of selected options;
an example of usage:
<c-multi-select-pick-list options={marketAccessOptions}
onvaluechange={handleValueChange}
label="Account Industry">
</c-multi-select-pick-list>
handleValueChange(event){
console.log(JSON.stringify(event.detail));
}
To see the demo, let’s create a new component that will use LDS to get the Account Industry and then pass those as an option to our component.
Demo
<!--
@description :
@author : Amit Singh
@group :
@last modified on : 02-02-2021
@last modified by : Amit Singh
Modifications Log
Ver Date Author Modification
1.0 02-02-2021 Amit Singh Initial Version
-->
<template>
<lightning-card variant="Narrow" title="Mulit Select ComboBox" icon-name="custom:custom43">
<div class="slds-p-horizontal_small">
<c-multi-select-pick-list
if:true={picklistValues}
options={picklistValues}
onvaluechange={handleValueChange}
label="Industry">
</c-multi-select-pick-list>
</div>
</lightning-card>
</template>
/**
* @description :
* @author : Amit Singh
* @group :
* @last modified on : 02-02-2021
* @last modified by : Amit Singh
* Modifications Log
* Ver Date Author Modification
* 1.0 02-02-2021 Amit Singh Initial Version
**/
import { LightningElement, wire } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class LdsPicklist extends LightningElement {
picklistValues;
error;
@wire(getPicklistValues, {
recordTypeId: '012000000000000AAA', fieldApiName: INDUSTRY_FIELD
})
wiredPicklist({ error, data }){
if(data){
this.picklistValues = data.values;
console.log(' data ', data.values);
this.error = undefined;
}
if(error){
this.picklistValues = undefined;
this.error = error;
}
}
handleValueChange(event){
console.log(JSON.stringify(event.detail));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>50.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<!--<target>lightning__Tab</target>-->
<!--<target>lightning__Inbox</target>-->
<!--<target>lightning__UtilityBar</target>-->
<!--<target>lightning__FlowScreen</target>-->
<!--<target>lightningSnapin__ChatMessage</target>-->
<!--<target>lightningSnapin__Minimized</target>-->
<!--<target>lightningSnapin__PreChat</target>-->
<!--<target>lightningSnapin__ChatHeader</target>-->
<!--<target>lightningCommunity__Page</target>-->
<!--<target>lightningCommunity__Default</target>-->
</targets>
</LightningComponentBundle>
Happy Learning 🙂
Please do, like, share and subscribe to receive more updates.
#DeveloperGeeks #AskPanther #SfdcPanther
[…] Read more […]
Great work Amit !
Super. One question, when all selected i need to get that value in handler. How can i get it.
Please help
You need to handle valuechange event in the parent component which is being dispatched from the child component.
Hi Amith,
I have a requirement, when there is only one value coming to the picklist, i need to show that value as default value, not ‘All’. Could you please help me with code, i am new to LWC.
You have to send the value from parent component to child component and then push that value in the “value” arrays inside connectedCallback or renderedCallback.
Unfortunately, I can not provide the code due to time constraints
[…] this.value = []; } } } picklist.css .picklist-pos__align { padding: 8px 0 5px 0; } Reference : How to create a multi-select picklist using lwc Happy Coding !! Keep watching this space for more updates. Have a nice day 🙂 [polldaddy […]
Hi , how could we set initial value for the picklist. For Eg: I need to set Industry to IT at initial load, how could I achieve that?
Hi Mak,
Try making value attribute with @api and then try sending the values in it.
Plz add apex class also
There is no ApexClass for this.
Hi Amith,
I am trying to pass selected values from multipicklist to apex but unable to do it , can you please let me know how to pass list to apex
Are you getting any errors? Use console.log to see how the selected values are getting printed and based on that use the data type for the parameter in apex method.
sir i want to have a multiselect combobox for the list of salesforce standard awa custom objects throguh LWC how to do that .while clicking on the drop down , pls help me on this
You need to use Schema class namespace to get the list of all objects from apex and then use those to prepare the picklist. Here is the Link to the apex class.