Multi Select Lookup Component in Salesforce Lightning

cafeforce
cafeforce 8.5k Views 23 comments

Hey CF Users,

Multi Select lookup lightning component is easy to create and is re-usable.

Lightning comes with many pre-built components that are easy to use but still many components are missing by date. One of them is the multi-select lookup component. We will create this within a single component without using any child component or event. You can find the complete source code in this post.

Multi Select Lookup component Features:

  • Single component
  • Will work on any object
  • Spinner to show processing
  • Ability to set sObject Icon
  • Label & placeholder functionality
  • Ability to show error in the dropdown if any occurred.
  • Pills of selected records will be shown
  • You can pre-populate selected records t passing their Ids
  • Search functionality to filter results
  • You can set a number of records to show at once. Default is 5

Spinner 

A Spinner will be shown when you will enter a keyword in the search box until the search results are fetched and shown.

Multi Select Lookup Spinner

Demo GIF:

 

Step-1: Create an Apex controller

Our apex controller will fetch records on passing object and field name to it. Here we are using a wrapper class which will return records in the form of value and label.

Multi Select Lookup Search Bar

Create MultiSelectLookupController.cls Apex class.

Step-2: Now we will create the Multi Select Lookup lightning component.

For creating a new lightning component navigate to Developer Console > File > New > Lightning Component

Enter Name & Description and click Submit. Now component markup file will open y default. In the right sidebar, click on Controller, Helper, and Style to create those files too.

Now since all the files are created, we will write markup for our component I have used standard (Salesforce Lightning Design System) SLDS here for styling.

 

Markup for MultiSelectLookup.cmp

Now open the controller file MultiSelectLookupController.js and paste the below code

Now open the helper file MultiSelectLookupHelper.js and paste the below code.

Here helper calls the apex class method and fetches record data from there. Now finally paste the CSS in MultiSelectLookup.css file.

Step-3: Now we will call the component.

Finally, our component is ready to use. Now while calling this component, you need to pass the objectName and fieldName to this custom component. You can also set additional attributes.

multi select lookup salesforce 5

Lightning Icon

You can set any standard icon that lightning provides. To see the complete list of lightning icons Click Here.

multi select lookup with pills

Here you go, the easy and optimized multi-select lookup component is ready to use.

Also Check:

If you want a more optimized single-select custom lookup, check out: Custom Lookup Lightning Component

If you want an LWC custom lookup, check out: Custom Lookup in LWC

If you want a lightning-fast custom lookup, check Out: Lightning fast lookup lite

For any queries or suggestions regarding this post, comment below:
Cheers … 🙂

Share This Article
23 Comments
Subscribe
Notify of
guest

23 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Kate

Hi there, I’m a Lightning Component newbie and am not sure where in salesforce I should be putting the code to call the component. I’ve inputted all of the other sections but am unsure of where I’d input this code. Thank you for your helpful guide!

David Robitaille

I’m a business user admin and would love to be able to use something like this for as simple of an application as selecting multiple users to send an email to using an email action in a flow. I’ve poked around to try to add this component to be available within a flow, but it does not seem to be working, and as a non-developer without the bandwidth to focus on it, I’m not sure what next steps are. Have you considered tweaking this and publishing this out for use in screen flows? That would be a killer use case – why this is not a standard component is beyond me.

Dylan

Do you have a version of this specific component using Lightning Web Components instead of Aura?

Chaitanya

How to save the values in the database?
In the object, I created one multi select picklist to store the values, but not able to save. Can you please help.

Thomas C.

Hi Suyash, thanks for this post it’s really helpful.
I have a question link to Chaitanya’s.
I use your component in another but I want to send selected records to apex class in my parent component but I don’t know how to do it. I can’t add a list attribute and value to your component. Could you help me please ?

Anks

Hi Suyash,

thanks for sharing such reusable custom component which can be easily implemented in any org. But I got some minor issues.

1.Unable to scroll through the listed search suggestions, the suggestions disappear when user tries to scroll through the suggestions
2. Unable to clear all pills at a time when user clicks on Clear Fields button. X-icon is remaining.
3. Search suggestions list is getting displayed properly, but unable to make selections using up/down arrow key on keyboard (however selection though mouse cursor is working).

If you could help me to fix the same, that would be really appreciated.

Prerana

Hi Suyash, Thanks for sharing the above code.
I am able to display multiple records through this multiselect lookup component. But not able to save the multiple records in another object where I have implemented the component. ( Ex: There are 2 objects Obj A and Obj B, I have displayed records of B(look up component) in A) Please could you share the code to save the multiple records selected from multiselect_lookup when the Obj A is saved.

Richard Valencia

I have the same question as Kate did a year ago.

<c:MultiSelectLookup objectName=”Account” fieldName=”Name” label=”Account Name” iconName=”standard:account” placeholder=”Enter Value”/>

wherein salesforce should I be putting the code to call the component? I’ve inputted all of the other sections but am unsure of where I’d input this code.

Can you share a screenshot of where it would go?

I have included a screenshot of where I have it placed and the error I’m getting when looking up records

error when searching “Unexpected token: “From”

2022-03-21_17-06-34.jpg
Last edited 2 years ago by Richard Valencia
Richard Valencia

@Suyash
Thank you for the reply, I was able to get over the error and search for records. However, I do have another set of questions for you, hoping you can help.

  1. How can I pull only related records in my search? The component will live on the case object > the component needs to pull related Relationship records (custom object) of that Account selected on the case.
  2. How can we make the values stay on the record page after selecting the records? Currently, I’m able to pick the records however when I move on to different records and come back to the record I was working on the values I selected no longer appear.
Harikishore Soma

Thanks for building re-usable Multi-select lookup functionality. This logic helped implementing similar functionality in one of my project. But I have difficulity with Input box size retain same when screen size is Zoom in or out. Actually I am calling MultiSelectLook component from parnet component. Please suggest how to align MultiSelectLook component input box size should be same as parent component fields

Rishabh

I wish it could also give us an option to create a record which we don’t find by the search. Can you direct me to a way/resource to incorporate this in my solution?

ghulam mohayyudin

I wanted to show the selected contacts inside the search bar not in under the lightning:input is that posible if how? Thanks