Today's Offer - Salesforce Certification Training - Enroll at Flat 10% Off.

- Salesforce Blogs -

Salesforce Object Search Language (SOSL)

Use the Salesforce Object Search Language (SOSL) to construct text-based search queries against the search index.SOSL search a particular string from multiple objects. It returns always list object. The result lists are always returned in the same order as they were specified in the SOSL query.SOSL is also like SOQL but search record from multiple objects. We can search in numerous objects at same time using SOSL. In SOQL, we can query only one object at a time but in SOSL.SOSL statements cannot exceed 10,000 characters.

  • The SOSL query starts with the keyword ‘FIND’.
  • We can search for some specified string like ‘USGLOBAl’ in multiple objects at the same time.
  • It search record from entire data base.
  • The result of SOSL is a list of lists of sObjects “List<List<sObject>>”.
  • The returned result contains the list of sObjects in the same order as order mentioned in SOSL query.
  • If a SOSL query does not return any records for a specified sObject type, then search results include an empty list for that sObject.
  • The search string should be at least two characters long.
  • You can search text, email, and phone fields for multiple objects.
  • The maximum batch size is 2,000 records.
  • SOAP or REST calls
  • Apex statements
  • Visualforce controllers and getter methods
  • Schema Explorer of the Eclipse Toolkit

salesforce Curriculum

Read: Entry Level Jobs Description for Salesforce Developer & Admin

When to Use SOSL

Use SOSL when you don’t know which object or in which field the data resides.

  • Retrieve data for a specific term that you know exists within a field. SOQL can searches are faster and can return more relevant results.
  • It finds the search text efficiently from multiple objects whether they are related to each other or not.
  • Retrieve data that’s in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.

Performance Considerations

If your searches are too general, they are slow and return too many results. Use the following clauses to define efficient text searches.

Read: Future Growth of a Salesforce Developer
  • IN: Limits the types of fields to search, including email, name, or phone.
  • LIMIT: Defines the maximum number of rows to return.
  • OFFSET: Displays the search results on multiple pages.
  • RETURNING: It will search from that object and that fields which mentioned and return result.

Where can use SOSL:

  • SOSL is used if we don’t know in which object the data is present.
  • We can retrieve multiple objects and field values efficiently when the objects may or may not be related to each other.
  • We can search only on those fields whose data type is text, phone and Email.
  • We can use in classes but not in Triggers.
  • We cannot perform DML operation on search result.

free salesforce demo Syntax

Read: What Are Sharing Rules? How To Create Sharing Rules In Salesforce?

List<list<SObject>> accountSearchList =newList<List<SObject>>();
accountSearchList=[FIND {Joe Smith}IN Name Fields RETURNING 
lead (name, phone Where createddate = THIS_FISCAL_QUARTERLIMIT 20) ];
 list<lead > accs;
Example of  SOSL
Apex class
Public with sharing class SOSLdemoExample{
 Public List<Opportunity> optyList {get;set;}
 Public List<contact> conList{get;set;}
 Public List<account> accList{get;set;}

 Public String searchStr{get;set;}
   Public SOSLdemoExample(){

  Public void soslSearchmethod (){
   optyList = New List<Opportunity>();
   conList = New List<contact>();
   accList = New List<account>();
   if(searchStr.length() > 2){
   String searchStr1 = '*'+searchStr+'*';
   String searchQuery = 'FIND \'' + searchStr1 + '\' IN ALL FIELDS RETURNING  Account (Id,Name,type),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, 'Sory, no results returned with matching string..'));
   apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least three characters..'));
Visualforce Page
<apex:page controller="SOSLdemoExample">
<apex:form >
<apex:inputText value="{!searchStr}"/>
<apex:commandButton value="Search in Account, Contact, Opportunity" action="{!soslSearchmethod }" reRender="acct,error,oppt,cont" status="actStatusId"/>
<apex:actionStatus id="actStatusId">
<apex:facet name="start" >
<img src="/img/loading.gif"/>
<apex:outputPanel title="" id="error">
<apex:pageMessages ></apex:pageMessages>
<apex:pageBlock title="Accounts" id="acct">
<apex:pageblockTable value="{!accList }" var="acc">
<apex:column value="{!}"/>
<apex:column value="{!acc.Type}"/>
<apex:pageBlock title="Contacts" id="cont">
<apex:pageblockTable value="{!conList}" var="con">
<apex:column value="{!}"/>
<apex:column value="{!}"/>
<apex:pageBlock title="Opportunities" id="oppt">
<apex:pageblockTable value="{!optyList}" var="opty">
<apex:column value="{!}"/>
<apex:column value="{!opty.StageName}"/>

Output Salesforce Object Search Language (SOSL)

Read: How to Become Certified Salesforce Developer?

Salesforce Tutorial Overview

    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.

Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

5 days 02 Feb 2020


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

-1 day 27 Jan 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

6 days 03 Feb 2020


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

7 days 04 Feb 2020


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

0 day 28 Jan 2020


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

-1 day 27 Jan 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

-1 day 27 Jan 2020

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

3 days 31 Jan 2020


Search Posts


Receive Latest Materials and Offers on Salesforce Course