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
Mallik1221Mallik1221 

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
Best Answer chosen by Mallik1221
Meghna Vijay 7Meghna Vijay 7
Change this soql query to EmailTemplate et=[Select id from EmailTemplate where Name=:'Users-Please login']; 
Hope it helps, if it does mark it as solved.
Thanks

All Answers

Meghna Vijay 7Meghna Vijay 7
Hi,
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
Mallik1221Mallik1221
Hi thanks for the reply, email template exists.
The error is coming in this line,  List<User> uds =[SELECT Id, LastLoginDate, Email FROM User where IsActive=True];. Is there any wrong here?
Meghna Vijay 7Meghna Vijay 7
Did you try to run that query in query editor of dev console and see if you are getting any records? Because in my org it works just fine.
 
Neethu uNeethu u
Hi
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
Mallik1221Mallik1221
Yes it is working fine, im getting records but when im trying to schedule this whole schedule apex class, im getting following error.

 User-added image

 
Meghna Vijay 7Meghna Vijay 7
Can you send screenshot of error logs of the debug ? I don't think it should be coming on List<User> uds =[SELECT Id, LastLoginDate, Email FROM User where IsActive=True];. Because in this case the uds.Size() = 0, The one where it says on this Line ERROR 
Mallik1221Mallik1221
Hi im not getting any errors ,
User-added image

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
Meghna Vijay 7Meghna Vijay 7
Can you send screeshot of email template ? I know it's bothering you alot but last screeshot.
 
Meghna Vijay 7Meghna Vijay 7
Change this soql query to EmailTemplate et=[Select id from EmailTemplate where Name=:'Users-Please login']; 
Hope it helps, if it does mark it as solved.
Thanks
This was selected as the best answer
Mallik1221Mallik1221
Hi Meghna,
It worked, thanks for your help.

Regards
Malli