In our previous posts, we developed a fully functional Multiselect Picklist with performance enhancements, and accessibility improvements. Now, it’s time to integrate this component into a Salesforce LWC and see it in action. This post will guide you through these steps and show how to make the use of multiselect picklist we’ve built through out this series.
You can follow along with the full code from the Salesforce Custom Components.
Integrate Picklist Into Another LWC
To get started, we first import the custom field Communication_Preference__c, which we created on the Contact object, to demonstrate how the picklist works interactively. Next, we use getRecord and getFieldValue from uiRecordApi to fetch the field value for a specific Contact record, using the recordId. Thus allowing us to show the values for the current contact record.
Define HTML Template
Lets write the HTML for multi-select component and button with the associated properties and event handlers.
Handle Save and Value Change Events
Remember in our first post we were sending the valuechange
custom event whenever the picklist value change. We are going to listen to that event from this component and update our state variable.
We are going to save the picklist values using updateRecord
method.
Don’t forget to import the updateRecord
from lightning/uiRecordApi
. I was debugging the same for 15 minutes before finding out that I missed the import.
Conclusion
In this post, we demonstrated how to integrate the Multiselect Picklist component into other LWC, handle value change, and update the record.
To see the full code and more, visit the GitHub repository: Salesforce Custom Components.
Thank you for following along on this journey of building a multi-select picklist component. Stay tuned for more updates, and happy coding!