Professional Documents
Culture Documents
SOSL Queries
SOSL Queries
=============
SOSL --> Salesforce Object Search Language
Apex Provides a Search Engine called as "SOSL Queries", which are used to search
for a particular content in all the fields in the multiple objects at a time.
If any of the record is having the search keyword in any of the column in the
record, then SOSL Query will return the record automatically.
Note:
To search the records inside the object by using SOSL Query, we need to
enable the "Allow Search"(Search Status Section) Checkbox upon creating / editing
the object settings.
Note: SOSL Queries should be implemented with the help of "FIND" keyword.
Syntax:
FIND 'searchText*' IN ALL FIELDS RETURNING
Object1(<Col1>,<Col2>,<Col2>,...., <ColN>),
Object2(<Col1>,<Col2>,<Col2>,...., <ColN>)
Object3(<Col1>,<Col2>,<Col2>,...., <ColN>),
...
...
Object20(<Col1>,<Col2>,<Col2>,...., <ColN>)
Ex:
FIND 'searchText*' IN ALL FIELDS RETURNING
Account(<Col1>,<Col2>,<Col3>,.....,<ColN>),
Contact(<Col1>,<Col2>,<Col3>,.....,<ColN>),
Opportunity(<Col1>,<Col2>,<Col3>,.....,<ColN>)
Lead(<Col1>,<Col2>,<Col3>,.....,<ColN>),
Order(<Col1>,<Col2>,<Col3>,.....,<ColN>),
Position__C(<Col1>,<Col2>,<Col3>,.....,<ColN>),
Candidate__C(<Col1>,<Col2>,<Col3>,.....,<ColN>)
Governor Limits:
----------------
1. We can have max. of 20 SOSL Queries per Transaction.
2. Each SOSL Query can return max. of 2,000 Records.
3. Each SOSL Query can search for the content in max. of 20 objects.
4. DML operations can't be applicable on the SOSL Query returned records.
Always Static SOSL Queries will return the result in the form of
"List<List<SObject>>".
Syntax:
List<List<SObject>> <objectName> = [ SOSL Query ];
2. Dynamic SOSL Queries:
Syntax:
string searchQuery = 'SOSL Query';
Syntax:
List<List<SObject>> <objectName> = Search.Query(searchQuery);
UseCase:
========
Design a Visualforce Page, to Search for the specified content in multiple
objects and represent the records in the Tabular format.
(Note: Search for the Content in Account, Contact, Lead and Position Objects)
VF Page Code:
-------------
<apex:page controller="GlobalSearchRecordsController" setup="false" sidebar="false"
showheader="true" tabStyle="Customer__c" >
<apex:sectionHeader title="Search" subtitle="Global Search"/>
<apex:form >
<apex:pageblock title="Global Search Block">
<apex:pageblocksection title="Search Records" columns="2"
Collapsible="false">
<apex:inputtext label="Enter Search Text : " html-placeholder="Enter
Search Text" value="{!searchText}"/>
<apex:commandButton value="Search Records" action="{!SearchRecords}"
rerender="accBlock, conBlock, ldBlock,posBlock"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
Controller Class:
-----------------
public class GlobalSearchRecordsController
{
Public string searchText{get;set;}