Hello friends, today we will discuss Lightning Record Picker in LWC. It’s a powerful component that allows users to search, select, and link to Salesforce records, (like Lookup field) all within the comfort of your Lightning Web Component. Previously we used custom LWC lookup fields for this, and in this, we created a separate component. But now Salesforce has released a new component.
The Lightning Record Picker is a user interface component provided by Salesforce that allows users to search and select records from Salesforce objects such as Accounts, Contacts, Opportunities, and custom objects. It simplifies the process of record selection and can be used in various scenarios where users need to associate or link records.
Also, check this: Drag and Drop Fields in LWC
Key Highlights :
- User-Friendly: Simplify record selection for users, enhancing data accuracy and efficiency.
- Streamlined Workflow: Streamline processes by linking records directly within your LWC.
- Data Integrity: Ensure that related records are accurately associated with one another.
- Customization: Developers can configure the Lightning Record Picker to work with specific objects, define search filters, and even customize the appearance to match the application’s look and feel.
- Multi-Select mode: The Record Picker could support multi-select mode, enabling the selection of multiple records at once.
- No need for a custom lookup component.
Code :
recordPickerWithFilter.html:
<template> <lightning-card variant="Narrow" title="Lightning Record Picker in LWC" icon-name="standard:record_lookup"> <div class="slds-p-horizontal_small"> <lightning-record-picker label="Select a record" placeholder="Search..." object-api-name="Contact" onchange={handleChange} matching-info={matchingInfo} display-info={displayInfo} filter={filter}> </lightning-record-picker> <br/><br/> <h3> Selected Record Id : {id} </h3> </div> </lightning-card> </template>
recordPickerWithFilter.JS:
import { LightningElement } from "lwc"; export default class RecordPickerWithFilter extends LightningElement { id; //on type we do search record according below marching where clouse matchingInfo = { primaryField: { fieldPath: "Name" }, additionalFields: [{ fieldPath: "Email" }], }; displayInfo = { additionalFields: ["Email", "Title"], }; // filter Contacts having email filter = { criteria: [ { fieldPath: "Email", operator: "ne", value: "", } ], }; handleChange(event) { console.log(`Selected record: ${event.detail.recordId}`); this.id = event.detail.recordId; } }
recordPickerWithFilter.JS-meta.xml:
<?xml version="1.0"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>57.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__HomePage</target> </targets> </LightningComponentBundle>
14 comments
Hi Rijwan can we use this lightning record picker in the lightning data table instead of a custom component for lookup-related column.
Yes we can @Appu
Hi Rijwan thanks for the reply. How can we add this lightning record picker inside the lightning data table?
Hi @Appu, Let me try this one
Hi @Appu, I tried with lightning record picker in LWC datatable and its working fine, Can you please schedule a meeting by below link, I will guide you
https://topmate.io/rijwan_mohmmed
Hi Rijwan, I scheduled a meeting from the given link for today at 10:00 PM IST.
But did not get any meeting link. Kindly share the link if I missed it.
Hi @Appu, I got the your request. Join the meeting by topmate on given time.
Hi Rijwan , I think I added the wrong email address on Topmate for scheduling the meeting.
How can I correct the email address?
Login to Topmate and check your call
Share me your email Id I will send the meeting link
Thanks, a lot. My email address is sappu9899@gmail.com
Hi,
I have use case where I want to initialize the record picker while components loads and then user can change by typing in so How to initialize the ?
Hi Maulik, set a value in the record picker
Can you please help how can we include lightning record picker as one of the column in LWC datatable