Skip to content
Skip to content
SalesforceFox

SalesforceFox

Easy way Learning & Exploring Salesforce !

  • Home
  • All Blogs
  • How To
  • Tips & Tricks
  • Salesforce Quiz
  • Resources
  • About
Send Automatic Reminder Emails To Inactive Users

Send Automatic Reminder Emails to Inactive Users in Salesforce

Piyush LakhaniMarch 27, 2025March 29, 2025

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.

Table of Contents
[Open][Close]
  • Understand Business Use Case
  • Understand Automation Solution for Reminder Emails
    • Identify Inactive Users
    • Sending automated reminder emails
  • Set Auto-execution of Reminder Emails
  • Email Deliverability Setting
  • Conclusion

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

Automatically send reminder emails to Users after 90 days of Inactivity in Salesforce

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

EmailDeliverabilitySetting

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.

Share this Now :

Related Posts:

  • User Access Policy in Salesforce : The Smartest way to Automate User Management Jobs
    User Access Policy in Salesforce : The Smartest way…
  • How To Use Flow Approval Process instead of Classic Approval Process
    How to use Flow Approval Process In Salesforce ?
  • How To Zip & UnZip Files In Salesforce
    How to Zip & Unzip files Natively in Salesforce: Use…
  • Create Dynamic Job Scheduling Framework in Salesforce
    Create a Dynamic Job Scheduling Framework in…
Apex, Automation, Email Alerts

Post navigation

Previous: Mastering SOLID Principles in Apex: Write Cleaner, Scalable and Maintainable Code for Salesforce
Next: Best way to avoid Recursion in Apex Triggers

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Weekly Salesforce Quiz

Categories

  • Admin (8)
  • Apex (6)
  • Automation (7)
  • Best Practices (6)
  • Flow (5)
  • LWC (4)
  • Security (1)
  • SOQL (1)
  • Trigger (1)
  • Uncategorized (1)

Recent Posts

  • User Access Policy in Salesforce : The Smartest way to Automate User Management Jobs
  • Display Apex-Defined Collection Data directly into Screen Flow Data Table
  • How to Secure Your Salesforce Org: First Steps Against Cyber Attacks and Social Engineering
  • Make Salesforce Screen flows more Interactive with Toast Messages
  • How to Zip & Unzip files Natively in Salesforce: Use This Ready-to-Use ZIP Utility

Tags

Admin Apex Aura Component Automation Best Practices Development Email Alerts Flow Flow Approval Process How To Lightning Experience LWC Reports Scheduled Jobs Security SOQL Tips Trigger User Management

Archives

  • November 2025 (1)
  • October 2025 (1)
  • August 2025 (1)
  • July 2025 (1)
  • June 2025 (2)
  • May 2025 (1)
  • April 2025 (2)
  • March 2025 (2)
  • January 2025 (2)
  • December 2024 (1)
  • October 2024 (1)
  • September 2024 (1)
  • August 2024 (1)
  • June 2024 (1)
  • April 2024 (1)
  • March 2024 (1)
  • October 2023 (1)
  • Linkedin
  • Youtube
  • Privacy Policy
  • Terms of Use
  • Contact
Theme: BlockWP by Candid Themes.
© 2023-2026 | SalesforceFox.com