SOSL in Salesforce

Search functionality is a fundamental requirement for any business or application. Salesforce.com offers two basic approaches for this, using SOSL and SOQL. Let us discuss the SOSL approach in detail in this blog post.

SOSL in Salesforce - Table of Content

What is SOSL in salesforce?

SOSL will be used to search the text string all across the object and around the field. Salesforce Object Search Language is what you're looking for. It is capable of searching for a specific string throughout various items.

SOSL statements return a list of sObjects, with each list containing the search results for a specific sObject type. The outcome lists have always been decided to return in the direction determined in the SOSL query.

SOSL is often used to build text-based search queries that are then run against search index. In the following environments, we could indeed search email, text, and phone number fields for various Items through including custom objects because we have direct exposure to to include in a single instance:

  • Visualforce Controllers 
  • SOAP or REST calls
  • APEX Declarations

Wish to make a career in the world of salesforce? Start with HKR'S  salesforce online training !

Fetching data from the database:

The database is a collection of tables that hold data. Using SOQL and SOSL querying in Salesforce, it is simple to fetch or retrieve data from a database.

SOQL stands for Salesforce Object Query Language.

SOSL is an abbreviation for Salesforce Object Search Language.

The two languages mentioned above assist us in dealing with databases and retrieving the necessary data.

Logic = Apex Code.

Apex Code is made up of logic and the ability to fetch data.

Salesforce Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Why is the salesforce object search language?

  • It searches for certain phrases/words to make it easier for developers to find any record or data.
  • It only searches the name, phone, and email fields.
  • SOSL is an abbreviation for Salesforce Object Search Language.
  • It focuses on various Object levels.
  • SOSL statements return sObject's "list of lists."
  • Each list includes the search results for a specific subject Type.
  • The outcomes list has always been brought back in the direction determined in the SOSL query.
  • If an SOSL query returns no results for a specific sObject type, the search result contains a vacant list for that Object.

Writing SOSL:

A SOSL query begins with the FIND clause. We would then need to execute clauses to the query to filter the object type, data, and fields. We can also control what is returned. A SOSL query is enquired in the format shown below.

Get ahead in your career with our salesforce Tutorial  

Syntax for SOSL:

Find:It clarifies the text (words or phrases) to be searched.

In Search Group: This specifies the scope of the fields to be searched. It goes into greater detail about the search. It could have the following values:

  • NAME FIELDS 
  • EMAIL FIELDS 
  • PHONE FIELDS 
  • ALL FIELDS

Returning: This is the data that will be returned in the search results. Following this keyword, a list of one or more objects could be stipulated. If we miss this, the results will comprise the Ids of all sObjetcs discovered.

LIMIT: This determines the maximum number of rows that will be decided to return by the query. If this parameter is left blank, the default value is 200, which is the maximum number of rows which can be returned.

Subscribe to our youtube channel to get new updates..!

Difference between SOSl and SOQL:

  • SOQL allows us to query only one object at a time. SOSL allows us to query multiple objects at once.
  • SOQL can run queries on any field of any data type whereas SOSL can run queries on fields with the data types Name, Phone, and Email.
  • In SOQL DML operations cannot be performed on query results whereas in SOSL DML operations cannot be performed on search results.
  • In SOQL List as a return type and in SOSL List Of Lists is the return type.

Click here to get frequently asked Salesforce interview questions & answers

How to test the SOQL queries?

We can perform the test in two ways. They are:

  • Apex class and 
  • Anonymous window
Anonymous window:

Steps: Log in to the Salesforce Developer Console. Press the bottom arrow of the window, then select the "Query Editor" tab, enter the code, and click "Execute."

Processing of SOQL /SOSL in Apex class:

Steps in creation of SOQL Apex class:

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

Steps to create SOSL apex class:

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

Declaring of list of list type variable:

list → List

list<list> → List of List declaration

Program:

list<list> ListOfResults = new list<list>();

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 ListA = new list();

list ListB = new list();

ListA = ListOfResults[0];

ListB = ListOfResults[1];

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

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

Salesforce Training

Weekday / Weekend Batches

Output:

(

(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}

)

)

Conclusion:

In the above blog post we had discussed about the SOSL in salesforce in depth. Had any doubts drop yourquieres in the comment section to get them resolved. Happy learning!

Related Articles:

Find our upcoming Salesforce Training Online Classes

  • Batch starts on 5th Dec 2021, Weekend batch

  • Batch starts on 9th Dec 2021, Weekday batch

  • Batch starts on 13th Dec 2021, Weekday batch

Global Promotional Image
 

Categories

Request for more information

Saritha Reddy
Saritha Reddy
Research Analyst
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.