• Indson

Difference Between SOQL and SOSL in Salesforce

Updated: Oct 24, 2021

SOQL (Salesforce Object Query Language):

  • SOQL is a Salesforce Object Query Language, retrieves the records from the database by using the SELECT keyword.

  • SOQL is used if we know in which object the data is present.

  • In SOQL we can query data from a single object and as well as multiple objects that are related to each other.

  • We can query on all fields of any datatype.

  • We can use SOQL in Triggers and classes.

  • We can perform DML operations on query results.


SOSL (Salesforce Object Search Language):


  • SOSL is a Salesforce Object Search Language, retrieves the records from the database by using the FIND keyword.

  • SOSL is used if we don’t know in which object the data is present.

  • We can retrieve multiple objects and field values efficiently when the objects may or may not be related to each other.

  • We can query only on fields whose data type is text, phone, and Email.

  • We can use it in classes but not in Triggers.

  • We cannot perform DML operations on the search results.

SOQL

SOSL

Salesforce Object Query Language

Salesforce Object Search Language

List Views, Reports, Apex

(Global, Sidebar, Advanced) Search, Apex

Synchronously (Can have Custom Indexes or Standard Indexes)

Happens Asynchronously. Usually 2-3 min. If over 9000 records are loaded at one time, the excess is moved to the bulk index queue. (slower).

Accuracy. Gives full set of results that match criteria.

​Relevance & Speed. Similar to Google Search. Weightage placed on recently viewed records.

Can search 1 object at a time.

Can search multiple objects at a time.

We can Query on only one table.

​We can query on multiple tables.



SOQL Statements SOQL statements evaluate to a list of sObjects, a single sObject, or an Integer for count method queries. For example, you could retrieve a list of accounts that are named Acme: List<Account> aa = [select id, name from account where name = 'Acme']; From this list, you can access individual elements: if (!aa.isEmpty()) {// Execute commands} You can also create new objects from SOQL queries on existing ones. The following example creates a new contact for the first account with the number of employees greater than 10: Contact c = new Contact(account = [select name from account where NumberofEmployees > 10 limit 1]); c.FirstName = 'James'; c.LastName = 'Yoyce'; Note that the newly created object contains null values for its fields, which will need to be set. The count method can be used to return the number of rows returned by a query. The following example returns the total number of contacts with the last name of Weissman: Integer i = [select count() from contact where Lastname = 'Weissman']; You can also operate on the results using standard arithmetic: Integer j = 5 * [select count() from account];

SOSL Statements SOSL statements evaluate a list of lists of objects, where each list contains the search results for a particular subject type. The result lists are always returned in the same order as they were specified in the SOSL query.SOSL queries are only supported in Apex classes and anonymous blocks. You cannot use a SOSL query in a trigger. If a SOSL query does not return any records for a specified subject type, the search results include an empty list for that sObject. For example, you can return a list of accounts, contacts, opportunities, and leads that begin with the phrase map: List<List<SObject>> searchList = [FIND 'map*' IN ALL FIELDS RETURNING Account (id, name), Contact, Opportunity, Lead]; Account [] accounts = ((List<Account>)searchList[0]); Contact [] contacts = ((List<Contact>)searchList[1]); Opportunity [] opportunities = ((List<Opportunity>)searchList[2]); Lead [] leads = ((List<Lead>)searchList[3]); Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

SOQL and SOSL have different indexes: SOQL indexes are:

  • Primary keys (Id, Name, and Owner fields)

  • Foreign keys (lookup or master-detail relationship fields)

  • Audit dates (such as LastModifiedDate)

  • Custom fields marked as External ID or Unique.

Fields that can’t be indexed in SOQL are:

  • Multi-select picklists

  • Currency fields in a multicurrency organization