Query Records Created Today or Yesterday with SOQL in Salesforce

In Salesforce, we can query records based on created or modified today or yesterday, as well as those with information related to tomorrow’s date, by appending TODAY, YESTERDAY, or TOMORROW to the WHERE clause in the SOQL query.

In this Salesforce tutorial, I will explain how to query records of standard and custom objects using date literals such as ‘today’, ‘tomorrow’, or ‘yesterday’ in Salesforce SOQL.

Query Records by Today Filter in Salesforce SOQL

To query the records created or updated on today’s date, we can execute the following SOQL query.

Query Account records created today:

For example, you have to query the account records created on today’s date, and then we can use the SOQL query below.

SELECT Id, Name, Type, Phone FROM Account WHERE CreatedDate = TODAY

After executing the above SOQL query, the account records you created today will be returned in the query.

query Salesforce object records that were created today

Query Account records last modified today:

Similarly, when you have to query the account records updated on today’s date, execute the following SOQL query.

SELECT Id, Name, Type, Phone FROM Account WHERE LastModifiedDate = TODAY
Query Salesforce records modified Today

Query Account records that were deleted today:

In Salesforce, if a record is deleted, it remains in the Salesforce recycle bin for 15 days or until it is manually removed from the recycle bin. We can’t query records that were deleted today, such as those with the condition “IsDeleted = Today,” because the IsDeleted condition only accepts a boolean expression.

We can combine the IsDeleted filter with LastModifiedDate in the SOQL query to retrieve deleted records as of today’s date.

In the developer console, the query below might not work, so you can execute the query in Salesforce Workbench to query the deleted records today.

Select Id, name from Account where IsDeleted = True and LastModifiedDate = Today 

To execute the query in the Salesforce workbench, navigate to workbench.developerforce.com, then log in with your credentials, and in the next window, for the field “Jump to, ” select SOQL query, then choose the object to which you want to query the deleted records.

Enter the query in the query editor and ensure you have selected the radio button “Include deleted and archived records.”

Query Salesforce Account records that were deleted today

This way, we can query the object records that were created, updated, and deleted on today’s date.

Query Records by Tomorrow filter in Salesforce SOQL

The common use case of the Tomorrow filter in Salesforce SOQL queries is to query events for tomorrow or any opportunity with a close date set for tomorrow.

Query Opportunity records where the Close date is tomorrow:

For example, when you need to query opportunity records with a close date of tomorrow, we can execute the SOQL query in the following manner.

SELECT Id, StageName FROM Opportunity WHERE CloseDate = TOMORROW

Output:

SOQL to query Opportunity close date on tomorrow

We can experiment with the TOMORROW filter to query custom or standard date fields, such as expiration dates or deliveries that are scheduled for tomorrow’s date.

Query Records by Yesterday filter in Salesforce SOQL

To query any information related to the object, such as the created date or modified date, we can use the ‘Yesterday’ filter in SOQL queries.

For example, to query the account records that were last modified yesterday, we can execute the following SOQL query.

SELECT Id, Name, Phone FROM Account WHERE LastModifiedDate = YESTERDAY
Query records updated yesterday in Salesforce

If you all want to query the last activities related to the accounts modified yesterday, then we can execute the SOQL query below for this polymorphic relation.

SELECT Id, (SELECT, Subject, CreatedDate FROM ActivityHistories), Name, Phone FROM Account WHERE LastModifiedDate = YESTERDAY

Output:

SOQL date filter to query Yesterday records

Combining Today, Tomorrow, and Yesterday Date Filters in Salesforce SOQL

When you apply the filter conditions that require both Today’s and Tomorrow’s filters, we can add them to the SOQL query using the AND & OR operators.

For example, you need to query the account records created today and have an event (custom date) scheduled for tomorrow; then, we can execute the following SOQL query.

SELECT Id, Name, Type, Phone FROM Account WHERE CreatedDate = TODAY AND Event_Date__c = TOMORROW

Output:

Today and Tomorrow filters in Salesforce SOQL

This way, we can combine the date filters using the AND operator to combine the query conditions for TODAY and TOMORROW records.

Similarly, we can also use the OR operator to combine filter conditions.

For example, you must query the account records modified today and yesterday.

SELECT Id, Name, Type, Phone FROM Account WHERE LastModifiedDate = TODAY OR LastModifiedDate = YESTERDAY

Output:

Salesforce SOQL query TODAY filter

This way, we can combine data literals, such as TODAY, TOMORROW, and YESTERDAY, using AND-OR operators to define conditions in Salesforce SOQL queries.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.