Salesforce Query Languages - SOQL & SOSL

In Salesforce, both SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) are query languages used to retrieve data from the Salesforce platform, but they serve different purposes and have distinct syntaxes.

SOQL (Salesforce Object Query Language):

SOQL is a query language used to retrieve records from a single object or related objects in Salesforce. It's similar to SQL (Structured Query Language) and is tailored specifically for querying Salesforce data.

Key features of SOQL include:

  • SELECT Statement: Used to retrieve specific fields from records.
  • Filters and Conditions: WHERE clause allows filtering records based on specified conditions.
  • Relationship Queries: Allows querying related objects using relationship fields.
  • Aggregate Functions: Supports aggregate functions like SUM(), AVG(), MAX(), MIN(), and COUNT().
  • Ordering and Sorting: ORDER BY clause enables sorting results based on specified fields.

Example of a SOQL query:

SELECT Id, Name, Account.Name FROM Contact WHERE Account.Type = 'Customer' ORDER BY Name LIMIT 10

Here in this example, we're selecting the Id, Name, and the associated Account's Name from Contact records where the related Account's Type is 'Customer', ordering the results by Name, and limiting the number of returned records to 10.

SOSL (Salesforce Object Search Language):

SOSL is a search language used to perform full-text searches across multiple objects in Salesforce. It's designed for performing text searches rather than structured queries like SOQL.

Key features of SOSL include:
  • FIND Clause: Used to specify the search term or terms.
  • RETURNING Clause: Specifies which objects and fields to return in the search results.
  • IN Clause: Allows specifying specific objects to search within.
  • Limitations: SOSL doesn't support all types of conditions and filters like SOQL. It's primarily focused on text-based searches.

Example of a SOSL query:

FIND {searchTerm} IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, Name) LIMIT 10

In this SOSL query, we're searching for the {searchTerm} in all fields across all searchable objects (ALL FIELDS), and returning Id and Name fields from matched Account and Contact records, with a limit of 10 results.

In summary, SOQL is used for querying specific records and related objects in Salesforce, while SOSL is used for performing full-text searches across multiple objects. Each serves its own purpose and is suited to different use cases.

References to explore more about this - 



Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach