Record Picker

lightning-record-picker

A component that provides an input search field, that can search for Salesforce records.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App

The lightning-record-picker component allows you to search for a list of Salesforce Records that match search input. It uses the GraphQL wire adapter to search for records, displays the records, and allows you to select a record.

This example creates a record picker component that searches for Account objects.

The filter attribute specifies which records the component lists in the record picker. The value for filter is an object with a criteria property and an optional filterLogic property. The value for criteria is an array with a list of filter criteria objects.

This example filters records where the Website field equals "https://www.grenoble.fr" or null, the Name of the related Parent doesn't start with "Acme", and Type doesn't equal "Partner".

To define logic for the set of filter criteria objects in the criteria array, specify the filterLogic property in the filter object. If filterLogic is undefined, all criteria are applied with an AND logical operator by default. Valid operators are AND, OR, and NOT. The AND operator has higher precedence than OR. Parentheses are supported.

The example in Specify a Filter shows how to use filterLogic.

The filter object defines the criteria for individual filters. You can combine these filters using logical operators that you specify with the filterLogic property.

The filter object requires the following properties for each element in the criteria array.

NameTypeDescription
fieldPath (required)stringThe name of the field in the API to filter on. Cross-object references such as Account.Phone are supported.
operator (required)stringThe filter operator. Can be any of the supported types and operators listed in the following table.
value (required)stringThe value to filter against. If using a date filter, see the following table for accepted values.
Type FamilyField Typeeqneltgtltegteinninlikeincludesexcludes
BooleanCheckbox
Date and TimeDate
Date and TimeDate/Time
Date and TimeTime
IDID
NumberCurrency
NumberGeolocation
NumberLatitude
NumberLongitude
NumberNumber
NumberPercent
PicklistMulti-Select Picklist
PicklistPicklist
TextAuto Number
TextEmail
TextEncrypted Text
TextLong Text
TextPhone
TextRich Text
TextURL
Calculated FieldsFormula
Calculated FieldsRoll-up Summary

The like operator works similarly to the SOQL like operator, with % and _ wildcards. The % wildcard matches zero or more characters. The _ wildcard matches exactly one character.

For date types (Date, DateTime, and Time), value is passed as an object with either a value or literal property. Use value if referring to an absolute date, time, or datetime. Use literal if referring to a relative date or time.

A value must be passed as a string (not a JavaScript date) that corresponds to the type of field. Use the JavaScript methods in the following table to convert to the correct format. The API throws an error if the date format is incorrect.

Field TypeFormatJS Date Conversion
DateTime"2023-03-10T14:24:44.567Z"date.toISOString()
Date"2023-01-01"date.toISOString().slice(0, 10)
Time"14:29:50.659Z"date.toISOString().slice(11)

This example uses a Date value to filter results to a specific date.

A literal is a date literal that represents a relative range of time, such as last month, this week, or next year. The value for literal must be a value from the GraphQL API list of Literal Values for Relative Dates.

The following example uses the TODAY literal to filter results to today's date.

To customize the way record suggestions appear in the record picker, use the display-info attribute. The display-info attribute references an object that specifies the main field and an additional field to display. By default, the value of the name field of the target object is displayed.

This example sets the primary field to the name of the related account and adds Title as a display field in the returned record picker results.

The display-info referenced object (displayInfo in the example) has the following properties. One or both properties are required if using display-info. Cross-object fields such as Account.Name are supported for both primaryField and additionalFields.

NameTypeDescription
primaryFieldstringThe first field displayed in record suggestions. The default value is the name field.
additionalFieldsarrayAn extra field to display in record suggestions. The value for additionalFields is an array with a single string element representing the additional field to display. Only the first element of the array is used.

By default, the search is conducted on the name field of the target object. To look for a match to a different field or additional fields, specify the matching-info attribute. The matching-info attribute references an object that defines which fields to query for the search terms in the record picker.

Cross-object fields such as Account.Phone are supported.

The matching-info referenced object (matchingInfo in the example) has the following properties. One or both properties are required if using matching-info.

NameTypeDescription
primaryFieldobjectThe primary field used to query the search terms. Only Text and Formula (Text) fields are supported. Object must be in the format { fieldPath: 'Name' }.
additionalFieldsarrayThe additional fields used to query the search terms. You can only specify one additional field. Only Text and Formula (Text) fields are supported. Array must be in the format [ { fieldPath: 'Phone' } ].

You can specify a matching mode for both the primary field and the additional fields using the mode property as shown in the following example. Specifying a matching mode can be useful in addressing performance concerns when searching through large datasets within an org.

The mode property accepts two values:

  • startsWith: matches results that start with the search term
  • contains: (default) matches results that contain the search term

This example shows a matchingInfo config that queries records whose Name field starts with the search term or whose Phone field contains the search term.

The event fired when a record has been selected or cleared.

The change event returns the following parameter.

ParameterTypeDescription
recordIdstringReturns the ID of the selected record or null if none.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThe event doesn't bubble.
cancelablefalseThis event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composedfalseThe event doesn't propagate outside the component in which it was dispatched.

The event fired when Lightning Data Service returns an error.

The error event returns an object with the following parameters.

ParameterTypeDescription
error.messagestringThe error message.
error.outputstringThe error details.
error.errorCodestringThe error code having the following format: ERR_RP00x. See the Error Handling section to explore the possible error code values.

The event properties are as follows.

PropertyValueDescription
bubblesfalseThe event doesn't bubble.
cancelablefalseThis event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composedfalseThe event doesn't propagate outside the component in which it was dispatched.

The event fired when the focus is set.

PropertyValueDescription
bubblesfalseThe event doesn't bubble.
cancelablefalseThis event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composedfalseThe event doesn't propagate outside the component in which it was dispatched.

The event fired when the focus is removed.

PropertyValueDescription
bubblesfalseThe event doesn't bubble.
cancelablefalseThis event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composedfalseThe event doesn't propagate outside the component in which it was dispatched.

The event fired when the component stops loading and is rendered. To call a method on the component, use the ready event. For example, you can call this.template.querySelector('lightning-record-picker').focus(); using this event to put focus on the component after it finishes rendering.

PropertyValueDescription
bubblesfalseThe event doesn't bubble.
cancelablefalseThis event has no default behavior that can be canceled. You can't call preventDefault() on this event.
composedfalseThe event doesn't propagate outside the component in which it was dispatched.

lightning-record-picker can return blocking and non-blocking errors. When a blocking error occurs, the input is disabled and a red message appears. To handle these errors, change the component configuration to exit the blocking error state based on what specific error was thrown.

When a non-blocking error occurs, a red message appears but the user can still interact with the component. The user must change their search or reload the page to exit the non-blocking error state.

The following is a list of errors emitted by the lightning-record-picker component.

Error CodeError MessageDescriptionBlocking?
ERR_RP001Missing or invalid required object-api-name attributeThe object-api-name attribute is omitted or formatted incorrectly.
ERR_RP002lightning-record-picker supports only objects with a name field...The object-api-name refers to an sObject that doesn't provide a name field.
ERR_RP003Error calling getObjectInfo for <object-api-name>Object information can't be retrieved.
ERR_RP004Error with getRecord...The default selected record can't be retrieved.
ERR_RP005Invalid filter configuration.The filter configuration is invalid.
ERR_RP006Invalid display info configuration.Display info configuration is invalid.
ERR_RP007Invalid matching info configuration.Matching info configuration is invalid.
ERR_RP008GraphQL error messageSearch results can't be retrieved.
ERR_RP009Missing required label attribute.Label attribute isn't provided or is invalid. Valid values are any non-empty strings

Validation messages display when the component loses focus or when the component uses the reportValidity() method. Validation messages also display when a user types a search term but doesn't select a record, or the component is required and the user doesn't select a record.

Set a custom validation message with the setCustomValidity() method. Setting a message puts the component in an invalid state. The custom message isn't automatically displayed. Call reportValidity() to display the custom message.

The lightning-record-picker component only supports objects in the User Interface API with at least one name field as part of the nameFields array from the getObjectInfo response.

The fields that are displayed for the records may not be an exact match for the records returned by the API. The displayed record fields are determined by the primaryField and additionalFields properties set for display-info. If the matching-info properties are set to record fields that aren't used by display-info, the displayed records can confuse users because the matching fields aren't displayed.

When you search for results with a search term, wildcards characters ('%' and '_') are escaped in the search term.

lightning-record-picker doesn't support searching on encrypted fields, including encrypted fields configured in the matching-info attribute.

When configuring the primaryField property of the display-info attribute, we recommend that you pick a field that's marked as required in the sObject definition. Using a required field prevents displaying a blank selection when a record is selected.

lightning-record-picker uses GraphQL wire adapter and inherits any GraphQL limitations. For more information, see GraphQL Known Limitations.

When working with a public method on lightning-record-picker, make sure the component is fully rendered before calling the method. Don't call focus() from renderedCallback() as the component and its child components may not be fully rendered. For example, you can call the public method using the ready event instead.

In your JavaScript file, set the focus on the record picker. This example assumes you have only one lightning-record-picker component on the page.

Alternatively, use the lwc:ref directive with the ready event to locate the component.

The LWC Recipes GitHub repository contains code examples for Lightning Web Components that you can test in an org.

For a recipe that uses lightning-record-picker, see the c-record-picker-* components in the LWC Recipes repo.