SOSL Of Salesforce

SOSL(Salesforce Object Search Language) In Salesforce:

SOSL is used to construct the text-based search queries against the search index. We can search email, text and phone number fields for multiple Objects, by including custom objects, that we have access to and include in a single query in the below-mentioned environments:

  • Visualforce controllers.
  • REST or SOAP calls.
  • APEX Statements

Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course.

How to fetch data from the DataBase?

The database is a collection of tables which stores data in it. To fetch or retrieve data from a DataBase, it is simple by using SOQL & SOSL querying in Salesforce.

SOQL - Salesforce Object Query Language.
SOSL - Salesforce Object Search Language.

The above two languages help us to deal with DB, to fetch the required data.

Apex Code = Logic.
Apex Code = logic + Data Fetching Capability.

Introduction to SOSL - Salesforce Object Search Language:

      • It performs searching of some phrases/words for making easy for devs to find any record or data.
      • It searches something only in name, phone, email fields.
      • SOSL stand for Salesforce Object Search Language.
      • It is more focused on various Object level.
      • SOSL statements return the “list of lists” of sObject.
      • In this, each list contains the search results for a particular subject Type.
      • The results list are always returned in the same order as they were specified in the SOSL query.
      • If SOSL query does not return any records for a specified sObject type, the search result includes an empty list for that sObject.

How to write SOSL?

A SOSL query starts with FIND clause. We can then add required clauses for filtering the object type, data, fields by the query. We can also determine what is returned. In the below-mentioned format, a SOSL query is queried.

 MindMajix YouTube Channel

Syntax For SOSL Query:

FIND: it specifies the text(word or phrases) to search phrase.

In Search Group: This tells the scope of the fields to have the search. It defines the search in more detail. The possible values in it can have:

ALL FIELDS
NAME FIELDS
EMAIL FIELDS
PHONE FIELDS

Returning: It is the information to return in the search results. List of one or more than one objects can be specified after this keyword. If we skip this, then the results contain the Id’s of all sObjetcs found.

LIMIT: It specifies the maximum number of rows returned in the query. If this is unspecified, then the default value is 200, which is the largest number of rows that can be returned.

Checkout SalesForce Tutorial

Difference between SOQL and SOSL:

SOQlSOSL
Using SOQL we can make query only on one object at a time.Using SOSL we can make a query on many objects at a time.
We can query on all fields of any data type.We can query on fields whose data type is Name, Phone & Email.
We can not perform DML operations on query resultsWe cannot perform DML operations on search results.
Return Type: ListReturn Type: List Of Lists.

Where to Test These SOQL queries?

We have two ways to perform this,

      • Apex Class.
      • Anonymous Window

Anonymous Window:

Steps: Login to Salesforce → Developer Console → Press the bottom arrow of the window and select “Query Editor” Tab and place the code and “Execute”.

SOSL Query Example:

FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c(name)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c, Levis__c
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c(First_Name__c), Levis__c(Price__c)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c(First_Name__c), Levis__c(Price__c WHERE Price__c >1000)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c(First_Name__c LIMIT 2), Levis__c(Price__c WHERE Price__c >1000)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm__c(First_Name__c LIMIT 2), Levis__c(Price__c WHERE Price__c >1000 ORDER BY Price__c )

How to process SOQL/SOSL in APEX Class?

Steps to Create SOQL Apex Class:

Log in to Salesforce org → Developer Console → Ctrl + E → Write the code and execute.

Program#1 Example:

list<Levis__c > ListOfJean = new list<Levis__c >();
ListOfJean = [SELECT Price__c FROM Levis__c WHERE Price__c > 1000];
system.debug(‘The Result =’+ ListOfJean);

OUTPUT:

12:54:26:007 USER_DEBUG [3]|DEBUG|The Result =(Levis__c:{Price__c=1200, Id=a066F00001MGfdVQAT})

Steps to Create SOSL Apex Class:

Login to Salesforce org → Developer Console → Ctrl + E → Write the code and execute.

How to declare List Of List Type Variable:

      • list<sObject> → List
      • list<list<sObject>> → List of List declaration

Checkout Salesforce Interview Questions

Program#2 Example:

list<list<sObject>> ListOfResults = new list<list<sObject>>();
ListOfResults = [FIND 'Blonde' IN ALL FIELDS RETURNING Naukri_com_JobForm__c(First_Name__c LIMIT 2), Levis__c(Price__c WHERE Price__c > 500 ORDER BY Price__c)];
system.debug('The Results=' + ListOfResults );

//add additional code for List Seperation of Output

list<Naukri_com_JobForm__c> ListA = new list<Naukri_com_JobForm__c>();
list<Levis__c> ListB = new list<Levis__c>();

ListA = ListOfResults[0];
ListB = ListOfResults[1];

system.debug('The Result in List A =' + ListA);
system.debug('The Result in List B =' + ListB);

OUTPUT:

The Results=
(
(Naukri_com_JobForm__c:{First_Name__c=Blonde,Id=a006F000039j3UgQAI}
),
(Levis__c:{Price__c=1100, Id=a066F00001MGxGbQAL},
Levis__c:{Price__c=1400, Id=a066F00001MGxEzQAL},
Levis__c:{Price__c=1500, Id=a066F00001MGxGqQAL}
)
)

After Adding Additional Code for List Separation the Output is:

14:42:56:127 USER_DEBUG [11]|DEBUG|The Result in List A =
(Naukri_com_JobForm__c:{First_Name__c=Blonde, Id=a006F000039j3UgQAI})

14:42:56:127 USER_DEBUG [12]|DEBUG|The Result in List B =
(Levis__c:{Price__c=1100, Id=a066F00001MGxGbQAL}, 
Levis__c:{Price__c=1400, Id=a066F00001MGxEzQAL}, Levis__c:{Price__c=1500, Id=a066F00001MGxGqQAL})

In the next topic, we will discuss in-detail about “DML Operations In Salesforce”. Keep following us for more info on Salesforce Development / Programming.

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration TrainingSalesforce Lightning Training
Salesforce Advanced Developer TrainingSalesforce Developer Training
Salesforce IoT TrainingSalesforce App Builder Certification Training
Salesforce AppExchange TrainingSalesforce Service Cloud Training
and many more. 

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingNov 19 to Dec 04View Details
Salesforce TrainingNov 23 to Dec 08View Details
Salesforce TrainingNov 26 to Dec 11View Details
Salesforce TrainingNov 30 to Dec 15View Details
Last updated: 03 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles