Mastering Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) in Salesforce Apex — Apex Part 8

Mohammad Usman
3 min readMar 16, 2024

--

Salesforce, the world’s leading cloud-based CRM platform, offers powerful tools for querying and searching data: Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL). In this comprehensive guide, we’ll delve into the intricacies of both SOQL and SOSL, exploring their syntax, capabilities, best practices, and how to execute them in Apex.

Salesforce development

Understanding SOQL

What is SOQL?

Salesforce Object Query Language (SOQL) is a query language specifically designed for querying Salesforce data. It’s SQL-like but tailored to the Salesforce platform’s unique data model.

SOQL Syntax

SOQL queries follow a syntax similar to SQL but with Salesforce-specific objects and fields. A basic SOQL query structure looks like this:

SELECT field1, field2 FROM ObjectName WHERE condition

- `SELECT`: Specifies which fields to retrieve.
- `FROM`: Specifies the Salesforce object to query.
- `WHERE`: Filters records based on specified conditions.

Examples of SOQL Queries

Basic Query

SELECT Id, Name, AccountNumber FROM Account

Query with Condition

SELECT Name, Email FROM Contact WHERE AccountId = ‘001XXXXXXXXXXXXXXX’

Query with Relationship

SELECT Name, (SELECT LastName FROM Contacts) FROM Account

Advanced SOQL Features

Relationships

SOQL supports querying related objects using nested queries or relationship fields.

Aggregate Functions

SOQL provides aggregate functions like `COUNT()`, `SUM()`, `MAX()`, etc., for summarizing data.

Query Optimization

Using selective filters, limiting returned records, and indexing fields can optimize SOQL performance.

Exploring SOSL

What is SOSL?

Salesforce Object Search Language (SOSL) is a text-based search language for querying across multiple objects in Salesforce.

SOSL Syntax

SOSL queries have a different structure compared to SOQL. Here’s a basic SOSL query:

FIND {searchQuery} IN ALL FIELDS RETURNING ObjectName(field1, field2)

- `FIND`: Specifies the search query.
- `IN ALL FIELDS`: Searches across all fields of the specified objects.
- `RETURNING`: Specifies which fields to return.

Examples of SOSL Queries

Basic Search

FIND {Acme} IN ALL FIELDS RETURNING Account(Name)

Search with Filter

FIND {Acme} IN NAME FIELDS RETURNING Account(Name WHERE Industry = ‘Technology’)

Advanced SOSL Features

Query Scope

SOSL allows specifying search scope using `IN`, `WITH`, and `USING` clauses.

Highlighting Search Results

Results from SOSL queries can be highlighted to emphasize matched keywords.

SOSL Limitations

SOSL queries have limitations like a maximum of 2000 characters in the search query and a maximum of 20 objects in a single query.

Executing Queries in Apex

Using SOQL in Apex

SOQL queries can be executed in Apex using `Database.query()` or directly in SOQL query statements.

Dynamic SOQL

Dynamic SOQL allows constructing queries dynamically based on runtime conditions.

Batch SOQL

Batch SOQL enables querying large data sets efficiently by dividing the query into manageable chunks.

Using SOSL in Apex

SOSL queries in Apex are executed using the `Search` or `find()` methods of the `SOSL` class.

Handling SOSL Results

SOSL results are returned as lists of `List<SObject>` which need to be processed accordingly.

Best Practices

SOQL Best Practices

- Use selective filters to improve query performance.
- Utilize relationship queries for querying related objects.
- Avoid SOQL queries inside loops to prevent hitting governor limits.

SOSL Best Practices

- Optimize SOSL queries by specifying search scopes.
- Handle SOSL query results efficiently to avoid performance bottlenecks.
- Be mindful of SOSL limitations and design queries accordingly.

Resources for Further Learning

To further enhance your understanding of advanced Apex features and Salesforce development in general, here are some recommended resources:

- Salesforce Apex Developer Guide: The official Apex developer guide provides comprehensive documentation and examples for mastering Apex programming.
- Trailhead: Salesforce’s interactive learning platform offers a wide range of modules and trails on Apex development, asynchronous processing, integrations, and more.
- Salesforce Developer Blog: Stay updated with the latest news, tips, and best practices from Salesforce developers and experts through the official developer blog.
- Stack Exchange — Salesforce: Engage with the Salesforce community, ask questions, and share knowledge on Stack Exchange’s dedicated Salesforce platform.

Conclusion

Mastering Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) is essential for efficiently retrieving data from the Salesforce platform. By understanding their syntax, capabilities, and best practices, developers can build robust and scalable applications on Salesforce. Whether querying individual records with SOQL or performing text-based searches with SOSL, these tools empower developers to harness the full potential of Salesforce’s rich data model.

--

--

Mohammad Usman

Trailblazer | Transforming Businesses through Salesforce Expertise | Salesforce Technical Architect, Consultant & Developer | Technical Lead