You need to sign in to do that
Don't have an account?
kumar 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;
}
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;
}
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:
Inactive user:
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
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.
The above code will work for 2000 or more user and fullfill you requirements as well..
Thanks
karthik
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);
This is code is owrking find for me.
Thanks
karthik
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 });