SOSL Queries:
- Salesforce Object Search Language (SOSL) is a Salesforce search language that is used to perform text searches in records.
- Use SOSL to search fields across multiple standard and custom object records in Salesforce.
This is an example of a SOSL query that searches for accounts and contacts that have any fields with the word 'SFDC'.
List<List<SObject>> searchList = [FIND 'SFDC' IN ALL FIELDS
RETURNING Account(Name), Contact(FirstName,LastName)];
Differences and Similarities Between SOQL and SOSL
Like SOQL, SOSL allows you to search your organization’s records for specific information. Unlike SOQL, which can only query one standard or custom object at a time, a single SOSL query can search all objects.
Another difference is that SOSL matches fields based on a word match while SOQL performs an exact match by default (when not using wildcards). For example, searching for 'Digital' in SOSL returns records whose field values are 'Digital' or 'The Digital Company', but SOQL returns only records with field values of 'Digital'.
Steps:
In the Developer Console, open the Execute Anonymous window from the Debug menu.
Insert the below snippet in the window and click Execute.
// Add account and related contact
Account acct = new Account(
Name='SFDC Computing',
Phone='(415)555-1212',
NumberOfEmployees=50,
BillingCity='San Francisco');
insert acct;
// Once the account is inserted, the sObject will be
// populated with an ID.
// Get this ID.
ID acctID = acct.ID;
// Add a contact to this account.
Contact con = new Contact(
FirstName='Carol',
LastName='Ruiz',
Phone='(415)555-1212',
Department='Wingo',
AccountId=acctID);
insert con;
// Add account with no contact
Account acct2 = new Account(
Name='The SFDC Query Man',
Phone='(310)555-1213',
NumberOfEmployees=50,
BillingCity='Los Angeles',
Description='Expert in wing technologies.');
insert acct2;
Let’s try running the following SOSL example:
In the Developer Console, click the Query Editor tab.
Copy and paste the following into the first box under Query Editor, and then click Execute.
FIND {Wingo} IN ALL FIELDS RETURNING Account(Name), Contact(FirstName,LastName,Department)
The search query in the Query Editor and the API must be enclosed within curly brackets ({Wingo}). In contrast, in Apex the search query is enclosed within single quotes ('Wingo').
Basic SOSL Syntax
SOSL allows you to specify the following search criteria:
Text expression (single word or a phrase) to search for
Scope of fields to search
List of objects and fields to retrieve
Conditions for selecting rows in the source objects
Execute this snippet in the Execute Anonymous window of the Developer Console. Next, inspect the debug log to verify that all records are returned.
List<List<sObject>> searchList = [FIND 'Wingo OR SFDC' IN ALL FIELDS
RETURNING Account(Name),Contact(FirstName,LastName,Department)];
Account[] searchAccounts = (Account[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('Found the following accounts.');
for (Account a : searchAccounts) {
System.debug(a.Name);
}
System.debug('Found the following contacts.');
for (Contact c : searchContacts) {
System.debug(c.LastName + ', ' + c.FirstName);
}
Hi! nice blog indeed
ReplyDeleteCould you provide any other way to write this?
Account[] searchAccounts = (Account[])searchList[0];
Contact[] searchContacts = (Contact[])searchList[1];
I mean with parenthesis instead of square brackets
Thanks!
Got it! :)
ReplyDeleteList searchAccounts = searchList.get(0);
List searchContacts = searchList.get(1);