You need to sign in to do that
Don't have an account?

notify user by sending an email to user whose last-login is more than 1 day
Here is the apex class I wrote and scheduled but it is throwing an error.
global class LastLoginEmail2 implements Schedulable {
global void execute(SchedulableContext SC) {
List<User> uds =[SELECT Id, LastLoginDate, Email FROM User where IsActive=True];
EmailTemplate et=[Select id from EmailTemplate where Name=:'Users_Please_login'];
Messaging.SingleEmailMessage[] mails = new Messaging.SingleEmailMessage[0];
for(User u : uds){
If( u.LastLoginDate<=System.today().addDays(-1)){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(u.Id);
mail.setSenderDisplayName('Salesforce Support');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setTemplateId(et.id);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
}
Here is the error:
Scheduler: failed to execute scheduled job:class: common.apex.async.AsyncApexJobObject, reason: List has no rows for assignment to SObject
I tried it in different ways but still this error is coming, could anyone help me on this please.
Thanks
global class LastLoginEmail2 implements Schedulable {
global void execute(SchedulableContext SC) {
List<User> uds =[SELECT Id, LastLoginDate, Email FROM User where IsActive=True];
EmailTemplate et=[Select id from EmailTemplate where Name=:'Users_Please_login'];
Messaging.SingleEmailMessage[] mails = new Messaging.SingleEmailMessage[0];
for(User u : uds){
If( u.LastLoginDate<=System.today().addDays(-1)){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(u.Id);
mail.setSenderDisplayName('Salesforce Support');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setTemplateId(et.id);
mails.add(mail);
}
Messaging.sendEmail(mails);
}
}
}
Here is the error:
Scheduler: failed to execute scheduled job:class: common.apex.async.AsyncApexJobObject, reason: List has no rows for assignment to SObject
I tried it in different ways but still this error is coming, could anyone help me on this please.
Thanks
Hope it helps, if it does mark it as solved.
Thanks
All Answers
Is it coming on this line :- EmailTemplate et=[Select id from EmailTemplate where Name=:'Users_Please_login']; if yes then the email template does not exist with the same Name , make sure you write it with correct Name
Hope it helps, if it does mark it as solved.
Thanks
The error is coming in this line, List<User> uds =[SELECT Id, LastLoginDate, Email FROM User where IsActive=True];. Is there any wrong here?
Try checking the below things:
1. Try executing the below SOQL in developer console and check any records are coming as result. If its not returning any records, it is the issue
SELECT Id, LastLoginDate, Email FROM User where IsActive=True
2. In the below SOQL, you are mentioning ' : ' which is used in variable. Since you are using ' it is searching for a constant string variable
Select id from EmailTemplate where Name=:'Users_Please_login'
change it to Select id from EmailTemplate where Name='Users_Please_login'
Try these two things.
If it helps, please mark it as best answer.
Thanks,
Neethu
the main issue when im scheduling this class, it is throwing error after executing at schedule time.
My main aim is to write a apex class to notify user by sending an email to user whose last-login is more than 1 day. I had created email template and wrote this class, but is is throwing error.
Thanks
Malli
Hope it helps, if it does mark it as solved.
Thanks
It worked, thanks for your help.
Regards
Malli