Salesforce Object Search Language (SOSL) – SOQL and SOSL – Salesforce Certified Platform Developer I Study Guide

6.2  Salesforce Object Search Language (SOSL)

Salesforce Object Search Language (SOSL) is a query language specifically designed for searching records in Salesforce. It allows you to search across multiple standard and custom Salesforce objects simultaneously, making it a powerful tool for finding data that meets specific criteria. SOSL queries can be used in various contexts, such as Apex code, Lightning Web Components, and the Salesforce REST API. Here’s a basic explanation of how SOSL works with an example:

Let’s say you work for a company that uses Salesforce to manage customer data, and you want to find all records that mention the term “acme” in either the Account Name or Contact Name fields. You could use SOSL to perform this search. SOSL uses the FIND keyword to initiate a search and allows you to specify one or more search terms. Here’s a simple SOSL query for our example:

FIND {acme} IN ALL FIELDS RETURNING Account, Contact

When you execute this SOSL query, Salesforce will search for any records that contain the term “acme” in their fields within the specified objects (Account and Contact). The results will include both Accounts and Contacts that match the search criteria.

When a SOSL query returns multiple lists of sObjects, the result is a list of lists of sObjects. Each list contains the search results for a particular sObject type, and the result lists are always returned in the same order as they were specified in the SOSL query. To handle SOSL query results that contain multiple lists of sObjects, users can use nested for loops to iterate over the lists and extract the data they need. Here is an example of how to handle SOSL query results that contain multiple lists of sObjects:

List<List<SObject>> searchResults = [FIND ‘apple’ IN ALL FIELDS RETURNING Account (Id, Name), Contact (Id, FirstName, LastName)]; for (List<SObject> resultList : searchResults) {

for (SObject record : resultList) {

if (record instanceof Account) {

Account accountRecord = (Account) record; System.debug(‘Account Name: ‘ + accountRecord.Name);

  • Do something with the account record } else if (record instanceof Contact) {

Contact contactRecord = (Contact) record;

System.debug(‘Contact Name: ‘ + contactRecord.FirstName +

  • ‘ + contactRecord.LastName);
  • Do something with the contact record

}

}

}

In this example:

\ 1.\ The SOSL query searches for the term “apple” in all fields of both the Account and Contact objects with specifying specific fields to return.

\ 2.\ The searchResults variable contains the search results as a list of lists of SObjects, just like in the previous example.

\ 3.\ The code then iterates through the search results:

The outer loop iterates over each inner list, which corresponds to the results for either the Account or Contact object.

The inner loop iterates through each SObject (record) within the inner list.

\ 4.\ Inside the inner loop, it checks whether each record is an instance of the Account or Contact object using the instanceof operator.

\ 5.\ Depending on the type of the record, it casts the SObject to either an Account or Contact object and then performs a simple action: printing the Account Name or Contact Name using System.debug().

Keep in mind that SOSL is particularly useful for searching across multiple objects and is designed for searching, not for retrieving detailed record information. If you need to retrieve specific fields of records, you may need to perform subsequent queries using SOQL.