User Story
As a user, I want to be notified when a company holiday is approaching so that I can plan my work accordingly. The system should check the Holiday object and compare the current date to the start and end dates of all company holidays. If a holiday falls on the current date, then an email should be sent to all users with a notification about the holiday.
The Apex code for this will be a scheduled job that runs every day at a specified time. It will query the Holiday object to retrieve all company holidays and compare the current date to the start and end dates of each holiday. If the current date falls within a holiday period, then the code will send an email to all users with a notification about the holiday.
The email will include the name and date of the holiday and a message reminding users that the company will be closed on that day. The email will also encourage users to plan their work accordingly and ensure that any critical tasks are completed before the holiday.
Code
global class SendHolidayEmail implements Schedulable {
global void execute(SchedulableContext sc) {
// Query for holidays
List<Holiday> holidays = [SELECT Name, ActivityDate FROM Holiday];
// Check if today is a holiday
Date today = Date.today();
Boolean isHoliday = false;
for(Holiday h : holidays){
if(h.ActivityDate == today){
isHoliday = true;
break;
}
}
// Send email to all users if it's a holiday
if(isHoliday){
List<User> allUsers = [SELECT Id, Email FROM User WHERE IsActive = true];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {};
for(User u : allUsers){
toAddresses.add(u.Email);
}
mail.setToAddresses(toAddresses);
mail.setSubject('Holiday Notification');
mail.setPlainTextBody('Today is a company holiday.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}