About this question
I am currently developing a Salesforce application that can track project deadlines. How can I use the “system.today()” in Apex to ensure that the tasks are marked overdue if their due date is before today’s date?
I am currently developing a Salesforce application that can track project deadlines. How can I use the “system.today()” in Apex to ensure that the tasks are marked overdue if their due date is before today’s date?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Salesforce Expert
Answered on May 14, 2024
In the context of Salesforce, you can use the “system.today()” method to compare the due task with the current date to determine if they are overdue. Here is an example given below of how you can create a custom object, insert records, and then use “system.today()” in apex for managing deadline:-
// Define a custom object for project tasks
Public class ProjectTaskHandler {
// Method to create sample project tasks
Public static void createSampleTasks() {
List tasksToInsert = new List();
// Creating sample tasks with different due dates
tasksToInsert.add(new Project_Task__c(Name=’Task 1’, Due_Date__c=System.today().addDays(3), Status__c=’Pending’));
tasksToInsert.add(new Project_Task__c(Name=’Task 2’, Due_Date__c=System.today().addDays(5), Status__c=’Pending’));
tasksToInsert.add(new Project_Task__c(Name=’Task 3’, Due_Date__c=System.today().addDays(-2), Status__c=’Pending’));
// Insert the sample tasks
Insert tasksToInsert;
}
// Method to update task statuses based on due dates
Public static void updateTaskStatuses() {
List tasksToUpdate = [SELECT Id, Name, Due_Date__c, Status__c
FROM Project_Task__c
WHERE Status__c != ‘Completed’ AND Due_Date__c < ystem xss=removed xss=removed> <strong>Here is the java based example given below by using “java.time.LocalDate”:-</strong>
Import java.util.ArrayList;
Import java.util.List;
Import java.time.LocalDate;
Public class ProjectTaskHandler {
// Define a custom class for project tasks
Static class ProjectTask {
String name;
LocalDate dueDate;
String status;
ProjectTask(String name, LocalDate dueDate, String status) {
This.name = name;
This.dueDate = dueDate;
This.status = status;
}
}
// Method to create sample project tasks
Public static List createSampleTasks() {
List tasks = new ArrayList<>();
// Creating sample tasks with different due dates
Tasks.add(new ProjectTask(“Task 1”, LocalDate.now().plusDays(3), “Pending”));
Tasks.add(new ProjectTask(“Task 2”, LocalDate.now().plusDays(5), “Pending”));
Tasks.add(new ProjectTask(“Task 3”, LocalDate.now().minusDays(2), “Pending”));
Return tasks;
}
// Method to update task statuses based on due dates
Public static void updateTaskStatuses(List tasks) {
List tasksToUpdate = new ArrayList<>();
// Iterate through tasks to update their status if due date is past
For(ProjectTask task : tasks) {
If(task.dueDate.isBefore(LocalDate.now()) && !task.status.equals(“Completed”)) {
Task.status = “Overdue”;
tasksToUpdate.add(task);
}
}
// Update tasks with the overdue status
For(ProjectTask task : tasksToUpdate) {
System.out.println(“Updating status of task “ + task.name + “ to “ + task.status);
// Code to update task status in the database or any other storage
}
}
// Main method for testing
Public static void main(String[] args) {
List sampleTasks = createSampleTasks();
updateTaskStatuses(sampleTasks);
}
}Here Is the Python programming language based example given below by using the “datetime” module for date comparison:-
From datetime import datetime, timedelta
# Define a custom class for project tasks
Class ProjectTask:
Def __init__(self, name, due_date, status):
Self.name = name
Self.due_date = due_date
Self.status = status
# Method to create sample project tasks
Def create_sample_tasks():
Tasks = []
# Creating sample tasks with different due dates
Tasks.append(ProjectTask(“Task 1”, datetime.now() + timedelta(days=3), “Pending”))
Tasks.append(ProjectTask(“Task 2”, datetime.now() + timedelta(days=5), “Pending”))
Tasks.append(ProjectTask(“Task 3”, datetime.now() – timedelta(days=2), “Pending”))
Return tasks
# Method to update task statuses based on due dates
Def update_task_statuses(tasks):
Tasks_to_update = []
# Iterate through tasks to update their status if due date is past
For task in tasks:
If task.due_date < datetime xss=removed xss=removed xss=removed xss=removed>Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Step-by-step Salesforce guides from industry experts
Common Salesforce interview questions, answered
Guides, tips and career advice on Salesforce from JanBask experts.
Salesforce How to Prepare for Salesforce Developer Certification: A Practical Guide
A step-by-step guide to Salesforce Developer certification - what to study for PD1, how PD2 fits in, and how to know you're ready…
Salesforce Salesforce Developer Certification Guide: Everything You Need to Know in 2026
Learn about Salesforce Developer Certification, including exam requirements, cost, topics, preparation time, retakes, and career…
Salesforce How to Prepare for Salesforce Admin Certification: A Practical Study Guide
Get a practical study plan for the Salesforce Admin exam - weighting by section, how to use Trailhead effectively, and how to…
Salesforce Salesforce Developer Salary Guide: How much you can earn
Discover the real Salesforce developer salary in 2026 by experience, location, skills & certifications. See what actually boosts…