Does your Organization have policy to deactivate or freeze users who haven’t logged on in 90 or certain X days ? Want to send them reminder ? Let’s see how we can send automatic reminder emails to inactive users who haven’t logged on since a while.
Like many trailblazers, You may also have multiple trailhead Playgrounds which can be deactivated if you you don’t login within 180 days. Well, you can easily send automatic reminders emails to prevent auto-deactivation. Let’s see how we can achieve this.
Understand Business Use Case
Consider your company wants you to monitor user engagement in its platform. However, you have found that many users, after sign up, use services a while & then become inactive. It’s your task to send them reminders to log back into the platform those who haven’t logged in within 90 or some certain days.
No way you are going to check inactive users manually and send them emails one by one, right ? So Let’s automate this.
Understand Automation Solution for Reminder Emails
Identify Inactive Users
First of all, Our first task is to find all inactive users who haven’t logged on since X numbers of days. Since these X days could vary on organization’s policy, we will make it dynamic.
public class sendEmailForUserInactivity {
Public static Integer reminderCutOffDays;
public static void checkUsersInactivity(Integer InactivitiesSinceDays) {
reminderCutOffDays = InactivitiesSinceDays;
DateTime cutoffDate = DateTime.now().addDays(-reminderCutOffDays); // find cutOff date
// Query admins users who have not logged in since X days
List<User> inactiveUsers = [SELECT Id, Name, Email, LastLoginDate
FROM User
WHERE Profile.Name = 'System Administrator'
AND LastLoginDate < :cutoffDate
AND IsActive = TRUE];
// If no inactive users, exit
if (inactiveUsers.isEmpty()) {
System.debug('No users found with inactivity over 180 days');
return;
}
}
}In above code, we have created class sendEmailForUserInactivity which contains method checkUsersInactivity. this method will return us list of inactive users.
This method takes one parameter, an integer, numbers of days since then users haven’t logged on. so using this parameters, We will calculate cuttOffDate and query all Inactive users based on that.
Sending automated reminder emails
Now, after we have list of Inactive user, Let’s make another method in which, we will get all those inactive users’ emails and send them reminder emails as shown below,
// Method to send emails in batch
private static void sendBatchEmails(List<User> inactiveUsers) {
String BaseURL = URL.getOrgDomainURL().toExternalForm();
// Define email subject and body
String subject = 'Login Reminder: Your Salesforce Org Will Be Deactivated Soon';
String body = 'Dear User,\n\nYour Salesforce account (' +BaseURL+ ') has not been accessed in the last ' +reminderCutOffDays+ ' days.\n\n' +
'Please log in soon to avoid deactivation.\n\nRegards,\nYour Salesforce Admin';
// Create a list to hold all the email messages
List<Messaging.SingleEmailMessage> emailMessages = new List<Messaging.SingleEmailMessage>();
for (User u : inactiveUsers) {
if (u.Email != null && u.Email != '') {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {u.Email});
mail.setSubject(subject);
mail.setPlainTextBody(body);
mail.setSenderDisplayName('Salesforce Admin');
// Add the email to the list
emailMessages.add(mail);
} else {
System.debug('User has no email address: ' + u.Id);
}
}
// Send the batch of emails in a single call
if (!emailMessages.isEmpty()) {
Messaging.sendEmail(emailMessages); // Send all emails at Once
}
}As seen in above code, first, we’ve set up Subject, Body and ToAddress (user’s email) of Email to be sent and then storing each email into emailMessages List.
At the end, We are sending all emails at once since it’s important to do bulkification of code to avoid Salesforce governors’ limit for sending email via SingleEmailMessage.
Let’s see How entire code looks like :
public class sendEmailForUserInactivity {
Public static Integer reminderCutOffDays;
public static void checkUsersInactivity(Integer InactivitiesSinceDays) {
reminderCutOffDays = InactivitiesSinceDays;
DateTime cutoffDate = DateTime.now().addDays(-reminderCutOffDays); // find cutOff date
// Query admins users who have not logged in since X days
List<User> inactiveUsers = [SELECT Id, Name, Email, LastLoginDate
FROM User
WHERE Profile.Name = 'System Administrator'
AND LastLoginDate > :cutoffDate
AND IsActive = TRUE];
// If no inactive users, exit
if (inactiveUsers.isEmpty()) {
System.debug('No users found with inactivity over 180 days');
return;
}
// Send emails in batch
sendBatchEmails(inactiveUsers);
}
// Method to send emails in batch
private static void sendBatchEmails(List<User> inactiveUsers) {
String BaseURL = URL.getOrgDomainURL().toExternalForm();
// Define email subject and body
String subject = 'Login Reminder: Your Salesforce Org Will Be Deactivated Soon';
String body = 'Dear User,\n\nYour Salesforce account (' +BaseURL+ ') has not been accessed in the last ' +reminderCutOffDays+ ' days.\n\n' +
'Please log in soon to avoid deactivation.\n\nRegards,\nYour Salesforce Admin';
// Create a list to hold all the email messages
List<Messaging.SingleEmailMessage> emailMessages = new List<Messaging.SingleEmailMessage>();
for (User u : inactiveUsers) {
if (u.Email != null && u.Email != '') {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {u.Email});
mail.setSubject(subject);
mail.setPlainTextBody(body);
mail.setSenderDisplayName('Salesforce Admin');
// Add the email to the list
emailMessages.add(mail);
} else {
System.debug('User has no email address: ' + u.Id);
}
}
// Send the batch of emails in a single call
if (!emailMessages.isEmpty()) {
Messaging.sendEmail(emailMessages); // Send all emails at Once
}
}
}Above Apex class can find Inactive users and send them reminders after X days. However, we don’t want you to run this class manually, right ! we want to automate it so that it runs everyday and does its job automatically.
Set Auto-execution of Reminder Emails
To automatically find inactive users and send reminders emails, we need to create Scheduler which runs above code on specific time we set.
so Let’s first create scheduler class as follows,
public class sendEmailForUserInactivity_Scheduler implements Schedulable {
public void execute(SchedulableContext context) {
// this method checks for inactivity and sends emails after 90 days of inactivities
sendEmailForUserInactivity.checkUsersInactivity(90);
}
}Now, let’s schedule this sendEmailForUserInactivity_Scheduler class to run every day mid-night in Salesforce. Go to Set up & type ‘Scheduled Job’ & then click on Schedule Job and add details as shown below

As seen above, Set Job Name as ‘Check User Inactivities & Send Email’, Select Apex class as sendEmailForUserInactivity_Scheduler, then select schedule using cron and provide cron ‘0 0 0 ? * *’ and click Save.
If you want to understand how to build above Cron for scheduled job, check our previous post here
Email Deliverability Setting
Before We are all set to go, make sure that you select ‘All email’ as Access level under Deliverability from Set up Setting, as show below

We are all set to go, Once you scheduled this job for everyday. This Automation will wake up every day mid-night & checks and finds which users haven’t logged on since X days and sends them reminders emails.
Conclusion
Using Apex and Scheduled-job, We can easily send automatic reminder emails to inactive users. In order to implement this solution into your org, You can directly copy+paste both apex classes shown above sendEmailForUserInactivity & sendEmailForUserInactivity_Scheduler. You can change numbers of Inactivity days (90 in above code) in scheduler class as per your requirement and as last step schedule this from Schedule Job in set up.




