Thursday, February 23, 2017

Visualforce - Beyond Basics Series 50 - Easy Global Search

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 ?

<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>
view raw gistfile1.txt hosted with ❤ by GitHub
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++;
}
}
}
view raw gistfile1.txt hosted with ❤ by GitHub


Any Screenshots ?




Walk through in a video ?


5 comments:

  1. I am getting this Error,
    Map key Type not found in map
    Error is in expression '{!rec[field]}' in component in page quicksearch
    Please Advice
    Thanks

    ReplyDelete
  2. Hi Harsh.. are you sure there's record in the system for your search keyword ?

    ReplyDelete
  3. thank u for giving this best information...we are giving this best salesforce Online Training Hyderabad

    ReplyDelete
  4. Oh man! This blog is sick! How did you make it look like this !
    https://www.kitsonlinetrainings.com/course/salesforce-online-training salesforce online training
    Angular JS training
    Selenium training
    DevOps training
    Pega training
    Servicenow training

    ReplyDelete

Thanks for reading my blog !