What is Global Search ?
In Salesforce, there's a standard global search functionality at the top Header. This will search all the text, email, phone fields in Objects & list them.
How to implement one ?
If you want to implement one, it's simple to do so. we need to use SOSL for fuzzy search on fields in specific objects.
Where is the Code ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="testclass1_23022017"> | |
<apex:sectionHeader title="Global Search Box"/> | |
<apex:form > | |
<apex:outputLabel value="Enter you search text here "/> | |
<apex:inputText value="{!searchStr}"/> | |
<apex:commandButton value="Go" action="{!startSearch}" rerender="DisplayArea"/> | |
</apex:form> | |
<apex:outputPanel id="DisplayArea"> | |
<apex:outputPanel rendered="{!results != Null}"> | |
<apex:pageBlock > | |
<!-- For each Object, like Account, Opportunity, Product2 --> | |
<apex:repeat value="{!results}" var="objName"> | |
<apex:sectionHeader subtitle="{!objName}"/> | |
<!-- For each row --> | |
<apex:pageBlockTable value="{!results[objName]}" var="rec" > | |
<!-- For each field --> | |
<apex:repeat value="{!rec}" var="field"> | |
<apex:column value="{!rec[field]}" headerValue="{!field}"/> | |
</apex:repeat> | |
</apex:pageBlockTable> | |
</apex:repeat> | |
</apex:pageBlock> | |
</apex:outputPanel> | |
</apex:outputPanel> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class testclass1_23022017 { | |
/* VF Bindings */ | |
Public String searchStr{get;set;} | |
Public Map<String, List<Map<String, String>>> results {get;set;} | |
/* Action Function */ | |
public void startSearch() { | |
List<List<sObject>> lsResults = [ | |
FIND | |
:searchStr | |
IN | |
ALL FIELDS | |
RETURNING | |
Account(Name, Type), | |
Opportunity(Name, StageName), | |
Product2(Name, ProductCode) | |
]; | |
Map<String, List<String>> objNamesAndFields = new Map<String, List<String>>{ | |
'Account' => new List<String>{ 'Name', 'Type'}, | |
'Opportunity' => new List<String>{ 'Name', 'StageName'}, | |
'Product2' => new List<String>{ 'Name', 'ProductCode'}}; | |
results = new Map<String, List<Map<String, String>>>(); | |
Integer count = 0; | |
for (String objName : objNamesAndFields.keySet()) { | |
results.put(objName, new List<Map<String, String>>()); | |
for (Integer idx = 0; idx < lsResults[count].size(); idx++) { | |
Map<String, String> recMap = new Map<String, String>(); | |
for (String field : objNamesAndFields.get(objName)) { | |
recMap.put(field, (String) lsResults[count][idx].get(field)); | |
} | |
results.get(objName).add(recMap); | |
} | |
count++; | |
} | |
} | |
} |
I am getting this Error,
ReplyDeleteMap key Type not found in map
Error is in expression '{!rec[field]}' in component in page quicksearch
Please Advice
Thanks
Hi Harsh.. are you sure there's record in the system for your search keyword ?
ReplyDeletethank u for giving this best information...we are giving this best salesforce Online Training Hyderabad
ReplyDeleteOh man! This blog is sick! How did you make it look like this !
ReplyDeletehttps://www.kitsonlinetrainings.com/course/salesforce-online-training salesforce online training
Angular JS training
Selenium training
DevOps training
Pega training
Servicenow training
I really enjoy the article.Thanks Again. Fantastic.
ReplyDeletejava online training hyderabad
Core java online training Hyderabad