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
Veerendar AellaVeerendar Aella 

I want to send warning email alerts to users who's last login greater than 90 days.

I want to send warning email alerts to users who's last login greater than 90 days. Please help.
Best Answer chosen by Veerendar Aella
Raj VakatiRaj Vakati
You need to create an apex scheduler class and send emails


Refer this link 

https://salesforce.stackexchange.com/questions/152882/deactivate-user-whose-last-login-is-more-than-30-days
https://developer.salesforce.com/forums/?id=9060G000000UasZQAS

All Answers

Raj VakatiRaj Vakati
You need to create an apex scheduler class and send emails


Refer this link 

https://salesforce.stackexchange.com/questions/152882/deactivate-user-whose-last-login-is-more-than-30-days
https://developer.salesforce.com/forums/?id=9060G000000UasZQAS
This was selected as the best answer
GauravendraGauravendra
Hi Veerendar,
You can try something like this
List<User> listUser = [Select Id,LastLoginDate,Email FROM User ];
        List<String> userEmail = new List<String>();
        for(User u : listUser) {
            if(u.LastLoginDate.addDays(90) < System.today() ) {
                userEmail.add(u.Email);
            }
        }
        
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        
        email.setToAddresses(userEmail);         
        email.setSubject('Test Mail');
        email.setHtmlBody('Hello, <br/><br/>This is the test mail that you generated.');
        try{
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }catch(exception e){
            apexpages.addmessage(new apexpages.message(apexpages.severity.error,e.getMessage()));
        }
Hope this helps!   
Veerendar AellaVeerendar Aella
Hi Raj,

I need a test class for the below apex class

global class EmailToUser implements Database.Batchable<sObject> 
{
List<String> UserEmailList = new List<String>();
    global Database.QueryLocator Start(Database.BatchableContext BC)
    {String query ='';
     User[] selectedUsers = [SELECT Id, email FROM User WHERE IsActive = TRUE AND Id NOT IN (Select UserId from LoginHistory WHERE LoginTime = LAST_N_DAYS:1)];
     //User[] selectedUsers = [SELECT Id, email FROM User WHERE IsActive = TRUE AND Id NOT IN (SELECT UserId FROM LoginHistory WHERE LoginTime = LAST_N_DAYS:30)];
        system.debug('users' +selectedUsers);
            
            return Database.getQuerylocator(query);
        
    }
    global void execute(Database.BatchableContext BC, List<User> scope)
    {
        for(User userobj:scope){
            userEmailList.add(userobj.email);
        }
    }
    
    global void finish(Database.BatchableContext BC)
    {
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(userEmailList);
        Mail.setSubject('Updating Subject');
        mail.setSaveASActivity(False);
        mail.setPlainTextBody('Sample Body');
        Messaging.sendEmail(new messaging.singleEmailMessage[] {Mail});
    }
}
Raj VakatiRaj Vakati
Use this code . you can not insert the data in Login History object. In that case you can try SeeAllData=True in your test class.


try this code
 
@isTest(seealldata=true)
public class EmailToUseTest {
    public static testMethod void Test1(){
        Test.startTest() ;
        Database.executeBatch(new EmailToUser());
        Test.stopTest() ; 
    }
    
}

 
Newbie999Newbie999
Hi all, I cannot find last login field on user. Will the above code work?