Definition
A Date Literal is a keyword in SOQL that represents relative dates dynamically, such as TODAY, YESTERDAY, LAST_WEEK, THIS_QUARTER, LAST_FISCAL_YEAR, or NEXT_N_DAYS:n. Date Literals automatically adjust based on the current date, making queries adaptive without manual date recalculation.
Real-World Example
a senior developer at TerraForm Tech recently implemented Date Literal to solve a complex business requirement that cannot be addressed with declarative tools alone. They implement Date Literal with proper error handling, write 98% test coverage, and document the solution for future maintainers. The code passes security review on the first attempt.
Why Date Literal Matters
Date Literals are SOQL keywords that represent relative dates dynamically. Common ones include TODAY, YESTERDAY, TOMORROW, LAST_WEEK, THIS_WEEK, NEXT_WEEK, LAST_MONTH, THIS_QUARTER, LAST_FISCAL_YEAR, NEXT_N_DAYS:n, LAST_N_DAYS:n, and many more. Each literal resolves to an actual date or date range when the query runs, based on the current date and the user's locale settings. This makes queries adaptive without requiring code changes when the date moves forward.
Date Literals are essential for any query that needs to capture data within a relative time window. Without them, you'd have to write Apex to calculate dynamic dates and substitute them into SOQL, which is awkward and error-prone. With Date Literals, the query reads naturally and stays correct as time passes. They're available in any SOQL context: Apex code, list views, reports, custom report types, and Workbench. The fiscal year variants (THIS_FISCAL_YEAR, LAST_FISCAL_YEAR) respect the org's fiscal year settings, which matters for financial reporting.
How Organizations Use Date Literal
- •Quantum Labs — Uses TODAY and LAST_N_DAYS:30 in their reports and SOQL queries so they always see current data without manual date updates.
- •TerraForm Tech — Built a scheduled batch job that uses LAST_WEEK to process the previous week's records every Monday. The batch is identical week to week because the date literal handles the date math automatically.
- •CodeBridge — Replaced hardcoded date strings in legacy Apex with date literals during a code review. The refactor eliminated the bug class where queries got stale because the hardcoded date wasn't updated.
