So today we will create Global Search Component in Lightning. Previously I have shared a search/Lookup component. But using that we can only search one object at a time. But sometimes we need to do search on multiple objects. So today we will create a Lightning Global Search Component. We can use this component to search on Custom as well as Standard objects.
As we know using SOQL we can only query one object at a time. While the SOSL allows us to do a search on multiple objects. So once the user starts typing a text, we are using SOSL to search and find the related records.
Now here we only have few attributes. If user want to make it required and what is the label they want to display. Once user starts typing we pass the value to the controller and display the result in another component.
Set<String> obNameSet = new Set<String>(); obNameSet.add('account'); obNameSet.add('contact'); obNameSet.add('opportunity'); obNameSet.add('lead'); obNameSet.add('quote'); obNameSet.add('pricebook');
So In Apex we have configured these standard objects and all custom objects. However you can control the object names in two ways. Firstly, pass the object name as an attribute from the parent component. Secondly, Use Custom setting or Custom Metadata records to store the details and from there you can refer in apex.
As we are using SOSL to search records so all the standard limits related to SOSL will be applied here. As this is a custom component as a result you can use this in the community or in Visualforce pages as well. So far we don’t have any standard Global Search component available for internal org. So this can be very helpful in many situations.
To display record detail I have used
lightning:tag but we can also use lightning:recordForm to display object details.
You can find complete code on my github repo here.
Do you want to add anything, let me know in the comments section. Happy programming 🙂