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:
Want to enhance your skills in dealing with the world's best CRM, enroll in our Salesforce Online Course.
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.
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.
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.
SOQl | SOSL |
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 results | We cannot perform DML operations on search results. |
Return Type: List | Return Type: List Of Lists. |
We have two ways to perform this,
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 )
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.
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.
Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts
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:
Name | Dates | |
---|---|---|
Salesforce Training | Nov 19 to Dec 04 | View Details |
Salesforce Training | Nov 23 to Dec 08 | View Details |
Salesforce Training | Nov 26 to Dec 11 | View Details |
Salesforce Training | Nov 30 to Dec 15 | View Details |
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.