Monday, 29 May 2017

SOSL Example to search Accounts,Contacts and Opportunity using custom controller.

What is SOSL?

Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to perform text searches in records. Use SOSL to search fields across multiple standard and custom object records in Salesforce. 
1. Use SOSL to search fields across multiple objects. SOSL queries can search most text fields on an object.

2. The SOSL query starts with the keyword ‘FIND’.

3. The result of SOSL is a list of lists of sObjects “List<List<sObject>>”.
    Ex--> List<List<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS RETURNING       Account(Name), Contact(FirstName,LastName)];

4. You can mention which fields to be returned and want to show on the page. Suppose performing search on three objects like          Account,Contact,Opportunity. Then you can mention like, to return list of Account with (Name,Type) field should be returned and for Contacts results (firstName, lastName,email) should be returned and same for Opportunity. 

5. The search string should be at least two characters long.

Click For Demo



Below is the code to perform search using SOSL by providing text at least two characters and click on the apex:command button.
It Will show error message if no match found , string character less than two.

Visualforce Page:

<apex:page controller="SOSLSearchController" showHeader="false" sidebar="false">
  <apex:form >
  <apex:inputText value="{!searchStr}" style="margin: 8px;"/>
    <apex:commandButton value="Search in Accounts, Contacts, Opportunities" action="{!searchSOSLMethod}" reRender="acct,error,oppt,cont" status="actStatusId"/>
    <apex:actionStatus id="actStatusId">
        <apex:facet name="start" >
            <img src="/img/loading.gif"/>                    
        </apex:facet>
    </apex:actionStatus>
  </apex:form>
 
    <apex:outputPanel title="" id="error">
     <apex:pageMessages ></apex:pageMessages>
     </apex:outputPanel>
 
    <apex:pageBlock title="Accounts" id="acct">
    <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.type}"/>
          <apex:column value="{!acc.Rating}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 
 <apex:pageBlock title="Contacts" id="cont">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  
 <apex:pageBlock title="Opportunities" id="oppt">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
   
</apex:page>

Apex Class:

Public class SOSLSearchController{
 Public List<Opportunity> optyList {get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
  
 Public String searchStr{get;set;}
  
  
  public void searchSOSLMethod()
  {
      optyList = New List<Opportunity>();
       conList = New List<contact>();
       accList = New List<account>();
       if(searchStr.length() > 1){
       String inputString = '*'+searchStr+'*';
       String searchQuery = 'FIND \'' + inputString + '\' IN ALL FIELDS RETURNING  Account (Id,Name,type,Rating),Contact(name,email),Opportunity(name,StageName)';
       List<List <sObject>> searchList = search.query(searchQuery);
       accList = ((List<Account>)searchList[0]);
       conList  = ((List<contact>)searchList[1]);
       optyList = ((List<Opportunity>)searchList[2]);
       if(accList.size() == 0 && conList.size() == 0 && optyList.size() == 0){
           apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Sorry, No results found with matching string..'));
           return;
       }
       }
       else
       {
           apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least 2 characters..'));
           return;
       }
  }
}

0 comments:

Post a Comment

If you have any doubts, please let me know.