SOSL is a select query that returns list of lists. That is, it can return list of multiple objects. It searches a parameter within fields fo multiple objects as specified in the query.
Use the Salesforce Object Search Language (SOSL) to construct text searches in the search() call, in Apex statements, in Visualforce controllers and getter methods, or the Schema Explorer of the Eclipse Toolkit.
Unlike SOQL, which can only query one object at a time, SOSL enables you to search text, email, and phone fields for multiple objects simultaneously.
Use SOSL when:
You don't know in which object or field the data resides and you want to find it in the most efficient way possible.
You want to retrieve multiple objects and fields efficiently, and the objects may or may not be related to one another.
You want to retrieve data for a particular division in an organization using the divisions feature, and you want to find it in the most efficient way possible.
Example: In the below example string 'test' is being searched in 4 objects Opportunity,account,Lead and contact. You need to specify in which fields it should search this string.For example, if you want it to search in name and description fields of account then it would look like account( name,description) The list of lists has list of objects in the order mentioned in the query. For example if you specify account as first object in query; then the account list will be available at 0th location of list of lists.
Public with sharing class soqlExpController{
Public List<Opportunity> optyList {get;set;}
Public List<Lead> leaList{get;set;}
Public List<contact> conList{get;set;}
Public List<account> accList{get;set;}
Public soqlExpController(){
}
Public void soslDemo_method(){
optyList = New List<Opportunity>();
leaList = New List<Lead>();
conList = New List<contact>();
accList = New List<account>();
List<List <sObject>> searchList = [FIND 'test' IN ALL FIELDS RETURNING Account (Id,Name,type),Contact(name,email),Opportunity(name,StageName),Lead(company,name,status) ];
accList = ((List<Account>)searchList[0]);
conList = ((List<contact>)searchList[1]);
optyList = ((List<Opportunity>)searchList[2]);
leaList = ((List<Lead>)searchList[3]);
}
}
Output:
Limits:
SOQL and SOSL generally have the same limitations, however according to the Governer Limit documentation:
**Description** **Limit** Total number of SOQL queries issued 100 Total number of SOQL queries issued for Batch Apex and future methods 200 Total number of records retrieved by SOQL queries 50,000 Total number of records retrieved by Database.getQueryLocator 10,000 Total number of SOSL queries issued 20 Total number of records retrieved by a single SOSL query 200
SOSL:
Use SOSL when:
Example:
In the below example string 'test' is being searched in 4 objects Opportunity,account,Lead and contact. You need to specify in which fields it should search this string.For example, if you want it to search in name and description fields of account then it would look like account( name,description)
The list of lists has list of objects in the order mentioned in the query. For example if you specify account as first object in query; then the account list will be available at 0th location of list of lists.
Visualforce page:
Controller:
Output:
Limits:
SOQL and SOSL generally have the same limitations, however according to the Governer Limit documentation:
**Description** **Limit**
Total number of SOQL queries issued 100
Total number of SOQL queries issued for Batch Apex and future methods 200
Total number of records retrieved by SOQL queries 50,000
Total number of records retrieved by Database.getQueryLocator 10,000
Total number of SOSL queries issued 20
Total number of records retrieved by a single SOSL query 200
Thanks,
Rockzz