Assumptions:
1) Password will expire in 90 days.
2) Reminder will be sent before 1 day.
1) Password will expire in 90 days.
2) Reminder will be sent before 1 day.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global class PasswordExpiryReminder implements Schedulable { | |
global void execute(SchedulableContext sContext) { | |
sendNotificationMail(); | |
} | |
global void sendNotificationMail() { | |
try{ | |
Messaging.SingleEmailMessage[] theEmails = new List < Messaging.SingleEmailMessage > (); | |
OrgWideEmailAddress fromMailAddress = [SELECT Id FROM OrgWideEmailAddress WHERE DisplayName='Do Not Reply']; | |
List < User > nUser = [SELECT ID, email, LastPasswordChangeDate FROM User WHERE isActive = true AND LastPasswordChangeDate != NULL AND LastPasswordChangeDate != NULL]; | |
List<EmailTemplate> eMailTemplate = [Select Id,Name from EmailTemplate WHERE Name = 'Password Expiry Notice for Last day']; | |
ID eMailTemplate1; | |
for(EmailTemplate et:eMailTemplate){ | |
if(et.Name=='Password Expiry Notice for Last day'){ | |
eMailTemplate1 = et.id; | |
} | |
/* | |
use else if incase of multiple templates | |
*/ | |
} | |
for (User tempUser: nUser) { | |
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); | |
Integer daysLeft = (90 - tempUser.LastPasswordChangeDate.date().daysBetween(System.today())); | |
if(daysLeft == 1) { //Code to prepare mails before 1 days | |
mail.setOrgWideEmailAddressId(fromMailAddress.Id); | |
mail.setTargetObjectId(tempUser.Id); | |
mail.setTemplateId(eMailTemplate1); | |
mail.saveAsActivity = false; | |
theEmails.add(mail); | |
} | |
/* | |
Use else if incase of multiple reminders | |
*/ | |
} | |
Messaging.Email[] allMails = new List < Messaging.Email > (); | |
If(theEmails.size() <> 0 && !theEmails.isEmpty()) { | |
for (Integer j = 0; j < theEmails.size(); j++) { | |
allMails.add(theEmails.get(j)); | |
} | |
} | |
If(allMails.size() <> 0 && !allMails.isEmpty()) { | |
Messaging.sendEmail(allMails); //Sending Emails to the Users | |
} | |
} | |
Catch(Exception E){ | |
E.getMessage(); | |
} | |
} | |
} |
Comments
Post a Comment