Diwali Deal : Flat 20% off + 2 free self-paced courses + Free Ebook + $200 Voucher  - SCHEDULE CALL

- Salesforce Blogs -

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

Read: A Basic Overview for Salesforce Security

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

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: Apex:Actionfunction In The Salesforce VisualForce Page

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

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: Salesforce Tutorial - Ultimate Learning Guide for Beginners & Beyond

 



Salesforce Tutorial Overview

fbicons FaceBook twitterTwitter lingedinLinkedIn pinterest Pinterest emailEmail

     Logo

    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.


  • fb-15
  • twitter-15
  • linkedin-15

Comments

Trending Courses

Cyber Security Course

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security Course

Upcoming Class

4 days 22 Nov 2024

QA Course

QA

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

Upcoming Class

14 days 02 Dec 2024

Salesforce Course

Salesforce

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

Upcoming Class

2 days 20 Nov 2024

Business Analyst Course

Business Analyst

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

Upcoming Class

5 days 23 Nov 2024

MS SQL Server Course

MS SQL Server

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

Upcoming Class

5 days 23 Nov 2024

Data Science Course

Data Science

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

Upcoming Class

4 days 22 Nov 2024

DevOps Course

DevOps

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

Upcoming Class

0 day 18 Nov 2024

Hadoop Course

Hadoop

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

Upcoming Class

4 days 22 Nov 2024

Python Course

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python Course

Upcoming Class

12 days 30 Nov 2024

Artificial Intelligence Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence Course

Upcoming Class

5 days 23 Nov 2024

Machine Learning Course

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning Course

Upcoming Class

39 days 27 Dec 2024

 Tableau Course

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau Course

Upcoming Class

4 days 22 Nov 2024

Search Posts

Reset

Receive Latest Materials and Offers on Salesforce Course

Interviews