- Salesforce Blogs -

Salesforce Object Search Language (SOSL)

  • Sep 13, 2017
  • 282.8k
  • 6 min read
  • Comment
Salesforce Object Search Language (SOSL)

Introduction

Use the Salesforce Object Search Language (SOSL) to construct text-based search queries against the search index. SOSL or Salesforce object query language searches for a particular string from multiple objects. It always returns a list object. The result lists are always returned in the same order as they were specified in the SOSL query. SOQL and SOQL in Salesforce are the most used language by developers.

SOSL (Salesforce Object Query Language) is similar to SOQL (Salesforce Object Query Language) but it can search for records from multiple Salesforce objects. We can search in numerous objects at the same time using SOSL. In SOQL, we can query only one object at a time but in SOSL, we can query multiple Salesforce objects together. To know more about Salesforce objects, you should study the Salesforce object model and how it works exactly. A salesforce object model helps you to map the data properly. Also, you can combine data logically as per the requirement.

Things to remember for Salesforce Object Search Language 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 Salesforce objects at the same time.
  • It searches records from the entire database. You can also combine SOQL and SOSL in Salesforce for more accurate search results.
  • 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 the order mentioned in the 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.
  • It can perform SOAP or REST calls
  • Apex statements are used for SOQL and SOSL in Salesforce.
  • Visualforce controllers and getter methods
  • Schema Explorer of the Eclipse Toolkit

Learn Salesforce in the Easiest Way

  • Learn from the videos
  • Learn anytime anywhere
  • Pocket-friendly mode of learning
  • Complimentary eBook available
Buy Self-learning at Discounted Price

Read: What is Scheduled Apex in Salesforce?

When to Use SOSL (Salesforce Object Search Language)?

  • 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 (Salesforce Object Query language) 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 are in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.

Performance Considerations for Salesforce Object Search Language 

If your searches are too general, they are slow and return too many results. Use the following clauses to define efficient text searches. You can also use SOQL (Salesforce Object Query language) if needed.

  • 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 particular object and fields that are mentioned and return the result.

Where can we use SOSL?

  • SOSL is used if we don’t know in which object the data is present. If you know about the object then you may prefer SOQL (Salesforce Object Query language) too.
  • We can retrieve multiple Salesforce objects and field values efficiently when the objects may or may not be related to each other.
  • We can search only in those fields whose data type is text, phone, and Email.
  • We can use SOSL in classes but not in Triggers.
  • We cannot perform DML operations on search results when using SOSL. At the same time, when using SOQL and SOSL in Salesforce, we can perform DML operations too.

Salesforce Training For Administrators & Developers

  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available
demo class

Syntax of Salesforce Object Search Language

List> accountSearchList =newList>();
accountSearchList=[FIND {Joe Smith}IN Name Fields RETURNING 
lead (name, phone Where createddate = THIS_FISCAL_QUARTERLIMIT 20) ];
 list accs;
accs=(list)result[0];
system.debug(accs);
Example of SOSL
Apex class
Public with sharing class SOSLdemoExample{
 Public List optyList {get;set;}
 Public List conList{get;set;}
 Public List accList{get;set;}
 
 Public String searchStr{get;set;}
   Public SOSLdemoExample(){
   }
 
  Public void soslSearchmethod (){
   optyList = New List();
   conList = New List();
   accList = New List();
   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> searchList = search.query(searchQuery);
   accList = ((List)searchList[0]);
   conList  = ((List)searchList[1]);
   optyList = ((List)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..'));
       return;
   }
   }
   else{
   apexPages.addmessage(new apexpages.message(apexpages.severity.Error, 'Please enter at least three characters..'));
   return;
   }
  }
}
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: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: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>

Output

Read: The Complete Guide on Asynchronous Apex in Salesforce

Salesforce Object Search Language (SOSL)

Salesforce Training For Administrators & Developers

  • Personalized Free Consultation
  • Access to Our Learning Management System
  • Access to Our Course Curriculum
  • Be a Part of Our Free Demo Class
Sing Up Now

Final Words

SOQL and SOSL in Salesforce are two popular languages to manage Salesforce objects, Standard objects and custom objects. Also, it is easy to retrieve data with the help of these two-programming languages. It may be tough learning about the syntax of the language and the Salesforce object model but the constant effort and dedication can make things easier than expectations. All the Best!

Read: Apex:Actionfunction In The Salesforce VisualForce Page

 

JanBask Training

Written by

JanBask Training

The JanBask Training Team includes certified professionals and expert writers dedicated to helping learners navigate their career journeys in QA, Cybersecurity, Salesforce, and more. Each article is carefully researched and reviewed to ensure quality and relevance.

View all blogs

Leave a comment

Keep reading

Related Salesforce Blogs

Explore more Salesforce blogs

More Salesforce articles to go deeper on this topic — tutorials, comparisons, and career guides from the same category.

Explore Career Programs Built for the AI Era

Choose from live, mentor-led programs designed around today’s fastest-growing technology skills and tomorrow’s AI-powered job roles.

Explore More

Salesforce Career Programs

Live, mentor-led Salesforce programs designed around the same skills this blog covers.

Explore Salesforce courses

Explore more topics

Browse Blog Categories

Jump to another subject — AI, Cloud, Salesforce, Data, Testing, and more — and keep learning by topic.

Interviews

Browse Categories