Functions

SOQL Date Literals

Using Date Literals

SOQL date literals like TODAY filter date fields.

Introduction to SOQL Date Literals

SOQL (Salesforce Object Query Language) date literals are a powerful feature that allows you to filter records based on date fields quickly and efficiently. These literals provide a convenient way to work with date-related data without needing complex logic or formula fields. In this guide, we'll explore the various date literals available in SOQL and how to use them in your queries.

Common SOQL Date Literals

SOQL offers several built-in date literals that can be used to filter records based on dates. Some of the most common date literals include:

  • TODAY: Represents the current day.
  • YESTERDAY: Represents the previous day.
  • TOMORROW: Represents the next day.
  • LAST_WEEK: Represents the previous week.
  • THIS_WEEK: Represents the current week.
  • NEXT_WEEK: Represents the next week.
  • LAST_MONTH: Represents the previous month.
  • THIS_MONTH: Represents the current month.
  • NEXT_MONTH: Represents the next month.

These literals can be directly used in SOQL queries to filter records on date fields.

Using Date Literals in SOQL Queries

Using date literals in SOQL is straightforward. You simply include the literal in your WHERE clause to filter records based on the date field of your choice. Here's how you can use them:

In the example above, we're selecting the Name and CloseDate fields from the Opportunity object where the CloseDate is equal to TODAY. This query returns all opportunities that are expected to close today.

This query retrieves events that started last week by using the LAST_WEEK date literal. Similarly, you can use other literals like THIS_MONTH or NEXT_YEAR to filter records for different timeframes.

Combining Date Literals with Other Conditions

Date literals can be combined with other conditions to create more complex queries. For example, you might want to find records that were created today and have a specific status:

In this case, the query retrieves all cases that were created today and are currently open. This demonstrates how date literals can be used alongside other conditions to refine your data retrieval further.

Advantages of Using Date Literals

Using date literals in SOQL offers several advantages:

  • Simplicity: Date literals eliminate the need for complex date calculations or formula fields.
  • Readability: Queries with date literals are easier to read and understand.
  • Performance: Date literals improve query performance by allowing Salesforce to optimize date comparisons internally.

Overall, SOQL date literals are an essential tool for any Salesforce developer looking to manage and analyze date-driven data efficiently.

Functions