function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Arvind_ChauhanArvind_Chauhan 

How to send alerts to users who haven't logged in for 60 days?

How can we send users an email notification automatically, who haven't logged into their Salesforce account for 60 days?
Sowjanya Hegde 13Sowjanya Hegde 13
Create a formula field on the  field in User object:
Label: User have not logged in
api name: User_have_not_logged_in__c
return type: number
formula: Today() - LastLoginDate

It will return the number of days since user have not logged in.
 
Write a class with the following logic:

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
List<User> usrList= [SELECT Id, LastLoginDate, Email, User_have_not_logged_in__c Name FROM User where User_have_not_logged_in__c >60];
List<String> sendTo = new List<String>();

    if(usrList.size()>0){
     for(User u:usrList){
      sendTo.add(u.Email);
    }
    
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      mail.setSenderDisplayName('Email alert');
      mail.setSubject('Owner change');
      String body = 'Dear User, Its been more than 60 days, you have not logged in to the Org';
      mail.setToAddresses(sendTo);
      mail.setHtmlBody(body);
      mails.add(mail);
      try{
      Messaging.sendEmail(mails);
      }
      catch(Exception e){
      system.debug('-------------exception------'+e);


You schedule it using schedule apex  if you want to run this class daily.
Malika Pathak 9Malika Pathak 9

Hi Arvind_Chauhan,

Please find the solution. How to send alerts to users who haven't logged in for 60 days?

global class remindUsers implements Database.Batchable<SObject>
{
 
    global Database.querylocator start(Database.BatchableContext bc)
    {
         dateTime dt = date.today()-60;
       String query = 'SELECT Name, LastLoginDate, Id From User WHERE IsActive = false AND LastLoginDate < +dt  and profile<>"System Adminstrator" ';
    
        
       return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext bc,List<User> activeUserList)
    {
        system.debug('activeUserList '+activeUserList);
        
    }

    global void finish(Database.BatchableContext bc)
    {
        AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email
                          FROM AsyncApexJob 
                          WHERE Id = :BC.getJobId()];

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {a.CreatedBy.Email};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Reminder of Your Login With salesforce ' + a.Status);
        mail.setPlainTextBody(' You have not login for 60 days Please login ' + a.TotalJobItems + ' batches with '+ a.NumberOfErrors + ' failures.');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

I hope it would work.

Please mark best soltuion so that other people would take reference from it.

Thanks

Arvind_ChauhanArvind_Chauhan
Thanks Sowjanya. I am new to Apex coding. I know how to create a formula field, but don't know where and how to implement and start using the apex code. Can you please help?
Arvind_ChauhanArvind_Chauhan
Thanks Malika for your solution as well, but as I mentioned above I am new to Apex coding. Don't know where and how to implement and start using the apex code. Can you please help?