Hello Trailblazers,
In this post, we're going to learn about lightning-record-picker which is basically an input field using which you can search for salesforce records. The basic code to implement the same is provided below:
<template> <lightning-card hide-header label="Account Record Picker Card"> <p class="slds-var-p-horizontal_small"> <lightning-record-picker label="Select Account" placeholder="Type Something..." object-api-name="Account" ></lightning-record-picker> </p> </lightning-card> </template>
As you can see above, I used the lightning-record-picker tag where I've setup the label as Select Account, the placeholder is Type Something... and the object-api-name is Account. You can ignore the lightning-card. I've added that only for a good white background as we're going to embed this component in our homepage. The result is shown below:
We also did common changes in our meta.xml file to embed this component in our homepage as shown below:
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>55.0</apiVersion> <isExposed>true</isExposed> <masterLabel>Record Picker Demo</masterLabel> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle>
Now, it's time to do some more changes. Let's begin!
Add filter to our lookup component
Let's add a default filter to our record picker component such that it'll search only those accounts whose Rating is equal to Warm. It's time to update our js file now:
import { LightningElement } from 'lwc'; export default class RecordPickerDemo extends LightningElement { filter = { criteria: [ { fieldPath: 'Rating', operator: 'eq', value: 'Warm' } ] }; }
As you can see above, we defined a filter object that consist of a single property named criteria. This property is an array which can have multiple objects, each having 3 properties:
1. fieldPath: API name of the field for the current object on which we've our record picker. You can also mention relationships upto one level, for example: Parent.Rating (considering the account object)
2. operator: It can have different values depending upon the comparison we want to perform. The possible values are given below:
eq = Equal
ne = Not Equal
lt = Less Than
gt = Greater Than
lte = Less than or equal
gte = Greater than or equal
in = Similar to IN operator of SOQL
nin = Similar to NOT IN operator of SOQL
like = Similar to LIKE operator of SOQL
includes = Check the result should include provided values
excludes = Check the result should exclude provided values
Different keywords provided above are applicable for fields of different data types. The fields and the operator values they support are provided in the salesforce documentation here. If you want to learn more about the keywords, you can check them in the GraphQL documentation here
3. value: Value for the applied filter
The updated html code to apply the filter we defined in js is provided below:
<template> <lightning-card hide-header label="Account Record Picker Card"> <p class="slds-var-p-horizontal_small"> <lightning-record-picker label="Select Account" placeholder="Type Something..." object-api-name="Account" filter={filter} ></lightning-record-picker> </p> </lightning-card> </template>
Notice that we've populated filter property of our lightning-record-picker with the filter variable that is defined in our js.
With this Rating filter applied by default, we can only see a subset of records. As you can see below, only 4 account records are present in my org with Rating as Warm
If I search in my lookup now, the search is performed with this predefined Rating filter already applied to my record picker:
import { LightningElement } from 'lwc'; export default class RecordPickerDemo extends LightningElement { filter = { criteria: [ { fieldPath: 'Rating', operator: 'eq', value: 'Warm' }, { fieldPath: 'Rating', operator: 'eq', value: 'Cold' } ], filterLogic: '1 OR 2' }; }
Display Additional Field in our Record Picker (Lookup) Search Results
import { LightningElement } from 'lwc'; export default class RecordPickerDemo extends LightningElement { filter = { criteria: [ { fieldPath: 'Rating', operator: 'eq', value: 'Warm' }, { fieldPath: 'Rating', operator: 'eq', value: 'Cold' } ], filterLogic: '1 OR 2' }; displayInfo = { additionalFields: ['Rating'] } }
<template> <lightning-card hide-header label="Account Record Picker Card"> <p class="slds-var-p-horizontal_small"> <lightning-record-picker label="Select Account" placeholder="Type Something..." object-api-name="Account" filter={filter} display-info={displayInfo} ></lightning-record-picker> </p> </lightning-card> </template>
Can we query using a different field?
By default, records are queried using the name field. However, we can use a different primary field to query records as well. We can also specify an additional field to query records. Let's say we want to query records using Rating field. The updated code for the js file is provided below:
import { LightningElement } from 'lwc'; export default class RecordPickerDemo extends LightningElement { filter = { criteria: [ { fieldPath: 'Rating', operator: 'eq', value: 'Warm' }, { fieldPath: 'Rating', operator: 'eq', value: 'Cold' } ], filterLogic: '1 OR 2' }; displayInfo = { additionalFields: ['Rating'] } matchingInfo = { primaryField: { fieldPath: 'Rating' } } }
If you notice above, I've defined a new object named matchingInfo. In this object, we can define primaryField as well as additionalFields that we want to use to query records. The primaryField is basically an object with single property named fieldPath which should have the API name of the field you want to use. For our example, the API name is Rating for the Rating field of account. Also, the updated html is shown below:
<template> <lightning-card hide-header label="Account Record Picker Card"> <p class="slds-var-p-horizontal_small"> <lightning-record-picker label="Select Account" placeholder="Type Something..." object-api-name="Account" filter={filter} display-info={displayInfo} matching-info={matchingInfo} ></lightning-record-picker> </p> </lightning-card> </template>
As you can see above, the matchingInfo property from our js is assigned to matching-info property of our lightning-record-picker.
Now, the records will be queried on the basis of Rating and not Name as shown below:
import { LightningElement } from 'lwc'; export default class RecordPickerDemo extends LightningElement { filter = { criteria: [ { fieldPath: 'Rating', operator: 'eq', value: 'Warm' }, { fieldPath: 'Rating', operator: 'eq', value: 'Cold' } ], filterLogic: '1 OR 2' }; displayInfo = { additionalFields: ['Rating'] } matchingInfo = { primaryField: { fieldPath: 'Rating' }, additionalFields: [ { fieldPath: 'Phone' } ] } }
Making the lightning-record-picker required
<template> <lightning-card hide-header label="Account Record Picker Card"> <p class="slds-var-p-horizontal_small"> <lightning-record-picker label="Select Account" placeholder="Type Something..." object-api-name="Account" filter={filter} display-info={displayInfo} matching-info={matchingInfo} required ></lightning-record-picker> </p> </lightning-card> </template>
Setting a default record using record id as the component is loaded
import { LightningElement } from 'lwc'; export default class RecordPickerDemo extends LightningElement { filter = { criteria: [ { fieldPath: 'Rating', operator: 'eq', value: 'Warm' }, { fieldPath: 'Rating', operator: 'eq', value: 'Cold' } ], filterLogic: '1 OR 2' }; displayInfo = { additionalFields: ['Rating'] } matchingInfo = { primaryField: { fieldPath: 'Rating' }, additionalFields: [ { fieldPath: 'Phone' } ] } recordId = '001H3000002jHRYIA2'; }
<template> <lightning-card hide-header label="Account Record Picker Card"> <p class="slds-var-p-horizontal_small"> <lightning-record-picker label="Select Account" placeholder="Type Something..." object-api-name="Account" filter={filter} display-info={displayInfo} matching-info={matchingInfo} required value={recordId} ></lightning-record-picker> </p> </lightning-card> </template>
That's all for this tutorial, I hope you liked it. There are also some predefined events linked to lightning-record-picker which you can check in the official documentation here. Let me know if you need a tutorial for the same and I can create one. I will look forward to your feedback in the comments down below.
Happy Trailblazing!!
No comments:
Post a Comment