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
kumar raj 7kumar raj 7 

Who ever not logged in 38 days i need to send email to respective users and after 45 days automatically inactive respective user?

Hi All,

Help me on this issue
I am trying to do now apex code.later i will start doing Batch apex.

public PageReference SendEmail()
    {
        List<user>userlist=[select id,name,email,lastlogindate from user where lastlogindate < last_N_days:38];
        List<Id> lstids= new List<Id>(); 
        for(User c:userlist) { 
            lstids.add(c.id);
        }         
        EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];  
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage(); 
        mail.setTargetObjectIds(lstids); 
        mail.setSenderDisplayName('System Admin'); 
        mail.setTemplateId(et.id); 
        Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail }); 
        return null;
    } 
karthikeyan perumalkarthikeyan perumal
Hello, 

you can do the above said thing using point and click method. why you need spacifically using apex? any reason behind this?..but   here is the code for send email and update user to inactive.

Send Email: 
 
        List<user> userlist=[select id,name,email,lastlogindate from user where lastlogindate < :DATE.TODAY()- 38];
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];       
        
		for(user u : userlist){
		
        Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
        mail.setTargetObjectIds(u.id); 		
        mail.setTargetObjectIds(); 
        mail.setSenderDisplayName('System Admin'); 
        mail.setTemplateId(et.id)
		mail.setSaveAsActivity(false)
        mails.add(mail);
		
		}
		
        Messaging.sendEmail(mails);

Inactive user:
  
        List<User> usersToUpdate = new List<User>();
        for(User u : [Select id, IsActive from User where  isActive=true and lastLoginDate < :DATE.TODAY()-45]){
        u.IsActive = false;
        usersToUpdate.add(u);
		}

		if (usersToUpdate.size()>0){
		update usersToUpdate;

		}


for point and click refere this links

https://success.salesforce.com/answers?id=90630000000gqBXAAY
https://success.salesforce.com/answers?id=90630000000CyjLAAS

hope this helps you.

Thanks
karthik
 
kumar raj 7kumar raj 7

Hello karthikeyan perumal,

Thanks for update!!.

 We have 2000 users are there in our organization.

Our Requirement: who ever not logged in 38 day we will send mail to users. after 45 days automatically user will be deactivated.
 

karthikeyan perumalkarthikeyan perumal
Hello, 

The above code will work for 2000 or more user and fullfill you requirements as well.. 

Thanks
karthik
 
kumar raj 7kumar raj 7
Hello Karthikeyan,

I am sending email also.Below code i am getting error.

List<user> userlist=[select id,name,email,lastlogindate from user where lastlogindate < :DATE.TODAY()- 38];
02List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
03EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];      
04 
05for(user u : userlist){
06 
07Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
08mail.setTargetObjectIds(u.id);     
09mail.setTargetObjectIds();
10mail.setSenderDisplayName('System Admin');
11mail.setTemplateId(et.id)
12mail.setSaveAsActivity(false)
13mails.add(mail);
14 
15}
16 
17Messaging.sendEmail(mails);
karthikeyan perumalkarthikeyan perumal
Hello, 

This is code is owrking find for me. 
 
List<user> userlist=[select id,name,email,lastlogindate from user where lastlogindate < :DATE.TODAY()- 38];
      List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
      EmailTemplate et=[Select id from EmailTemplate where name = 'EmailTemplatename' limit 1];       
      
		for(user u : userlist){

      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      mail.setTargetObjectId(u.id); 		
      //mail.setHtmlBody('test');
      mail.setSenderDisplayName('System Admin'); 
      mail.setTemplateId(et.id);
	  mail.setSaveAsActivity(false);
      mails.add(mail);

	}

      Messaging.sendEmail(mails);

Thanks
karthik
kumar raj 7kumar raj 7

Hello Karthikeyan,

This code is working fine


       List<Messaging.MassEmailMessage> listmail = new List<Messaging.MassEmailMessage>();
        List<Id> ids= new List<Id>();
        for(User u:[Select id,Email,IsActive,Name from User where isActive=true and lastLoginDate < :DATE.TODAY()-38])
        {
            ids.add(u.id);
        }
        Messaging.MassEmailMessage Email = new Messaging.MassEmailMessage();
        Email.setTargetObjectIds(ids);
        Email.setSenderDisplayName('System Admin');
        Email.setTemplateId('00X7F0000012bBD');
        Email.setSaveAsActivity(false); 
         
        Messaging.sendEmail(new Messaging.MassEmailMessage[] { Email });