function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
syamkishore1.3906536981992773E12syamkishore1.3906536981992773E12 

Could any one let me know when to use SOSL queries with some examples?

SFDC_DevloperSFDC_Devloper
Hi,

SOSL:
  • SOSL is a select query that returns list of lists. That is, it can return list of multiple objects. It searches a parameter within fields fo multiple objects as specified in the query.
  • Use the Salesforce Object Search Language (SOSL) to construct text searches in the search() call, in Apex statements, in Visualforce controllers and getter methods, or the Schema Explorer of the Eclipse Toolkit.
  • Unlike SOQL, which can only query one object at a time, SOSL enables you to search text, email, and phone fields for multiple objects simultaneously.

Use SOSL when:
  • You don't know in which object or field the data resides and you want to find it in the most efficient way possible.
  • You want to retrieve multiple objects and fields efficiently, and the objects may or may not be related to one another.
  • You want to retrieve data for a particular division in an organization using the divisions feature, and you want to find it in the most efficient way possible.

Example:
In the below example string 'test' is being searched in 4 objects Opportunity,account,Lead and contact. You need to specify in which fields it should search this string.For example, if you want it to search in name and description fields of account then it would look like  account( name,description)
The list of lists has list of objects in the order mentioned in the query. For example if you specify account as first object in query; then the account list will be available at 0th location of list of lists.

Visualforce page:
<apex:page controller="soqlExpController">
  <apex:form >
    <apex:commandButton value="Show records using SOSL" action="{!soslDemo_method}"/>
    <apex:pageBlock title="Accounts">
       <apex:pageblockTable value="{!accList }" var="acc">
          <apex:column value="{!acc.name}"/>
          <apex:column value="{!acc.Type}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
 <apex:pageBlock title="Contacts">
    <apex:pageblockTable value="{!conList}" var="con">
      <apex:column value="{!con.name}"/>
      <apex:column value="{!con.email}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
 <apex:pageBlock title="Leads">
    <apex:pageblockTable value="{!leaList}" var="lea">
      <apex:column value="{!lea.name}"/>
      <apex:column value="{!lea.company}"/>
    </apex:pageblockTable>
 </apex:pageBlock>
 <apex:pageBlock title="Opportunities">
    <apex:pageblockTable value="{!optyList}" var="opty">
      <apex:column value="{!opty.name}"/>
     <apex:column value="{!opty.StageName}"/>
 </apex:pageblockTable>
 </apex:pageBlock>
  </apex:form>
</apex:page>
Controller:
Public with sharing class soqlExpController{
 Public List<Opportunity> optyList {get;set;}
 Public List<Lead> leaList{get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}
   Public soqlExpController(){
   }
 
  Public void soslDemo_method(){
   optyList = New List<Opportunity>();
   leaList = New List<Lead>();
   conList = New List<contact>();
   accList = New List<account>();
   List<List <sObject>> searchList = [FIND 'test' IN ALL FIELDS RETURNING  Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName),Lead(company,name,status) ];
   accList = ((List<Account>)searchList[0]);
   conList  = ((List<contact>)searchList[1]);
   optyList = ((List<Opportunity>)searchList[2]);
   leaList  = ((List<Lead>)searchList[3]);
  }
}
Output:
User-added image


Limits:

SOQL and SOSL generally have the same limitations, however according to the Governer Limit documentation:

**Description**                                                                                                                 **Limit**
Total number of SOQL queries issued                                                                           100
Total number of SOQL queries issued for Batch Apex and future methods            200
Total number of records retrieved by SOQL queries                                                    50,000
Total number of records retrieved by Database.getQueryLocator                             10,000
Total number of SOSL queries issued                                                                            20
Total number of records retrieved by a single SOSL query                                         200


Thanks,
Rockzz