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
Akis AthanasiadisAkis Athanasiadis 

schedule email

Hi,

I need to send every day to the employees an email(reminder) to fill in yesterday's events in calendar.
I have used this code but it didn't work.
global class sendEmailToUSer implements Schedulable {
    global void execute(SchedulableContext sc) {
        list<User> lstUser = [Select ID from User where isActive = true and Name='Athanasiadis, Akis'];
        for(User iterator : lstUser) {
            EmailTemplate objTemp = [SELECT Id FROM EmailTemplate where DeveloperName = 'Case status for Contacts' limit 1];
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTemplateId(objTemp.Id);
            mail.setTargetObjectId(iterator.Id);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }
}
I received the following error:
Scheduler: failed to execute scheduled job: jobId: 7077E00000oGr5L, class: common.apex.async.AsyncApexJobObject, reason: List has no rows for assignment to SObject

I firstly wanted to check it on myself in order to make sure if this works.
Any help?
Best Answer chosen by Akis Athanasiadis
Akis AthanasiadisAkis Athanasiadis
Hi Pawan,

I used this code but I receive the following error:
Error: Compile Error: Method does not exist or incorrect signature: void setToAddresses(String) from the type Messaging.SingleEmailMessage at line 12 column 18

Regards,
Akis

All Answers

PawanKumarPawanKumar
Hi ,
I think your below query is returning no object so you are getting that error. in order to verify, just run your below query in developer console and see -query is returning anything or not.
 EmailTemplate objTemp = [SELECT Id FROM EmailTemplate where DeveloperName = 'Case status for Contacts' limit 1];

In addition, you can use List<EmailTemplate> objTempList , in order to avoid this failure and refer objTempList[0] in your setTemplateId method.

Apart from the above, you remove your email sending logic outside loop otherwise you will hit governer limit any time.

Regards,
Pawan Kumar
 
PawanKumarPawanKumar
Please find proper code as below.
--------------------------------------------------------------------------------

global class sendEmailToUSer implements Schedulable {
    global void execute(SchedulableContext sc) {
        // After replacing with (List<EmailTemplate>), At Least you will not get error what your were getting 
        // But Please check your email template query is returning the expected Template or not?
        List<EmailTemplate> objTempList = [SELECT Id FROM EmailTemplate where DeveloperName = 'Case status for Contacts' limit 1];

        // build all SingleEmailMessage
        List<User> lstUser = [Select ID from User where isActive = true and Name='Athanasiadis, Akis'];
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
        for(User iterator : lstUser) {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(iterator.email);
            mail.setTemplateId(objTempList[0].Id);
            mail.setTargetObjectId(iterator.Id);
            mailList.add( mail);
        }

        // send email here
        Messaging.sendEmail(mailList);
    }
}

Regards,
Pawan Kumar

PS: Please let me know if it helps.
Akis AthanasiadisAkis Athanasiadis
Hi Pawan,

I used this code but I receive the following error:
Error: Compile Error: Method does not exist or incorrect signature: void setToAddresses(String) from the type Messaging.SingleEmailMessage at line 12 column 18

Regards,
Akis
This was selected as the best answer
Akis AthanasiadisAkis Athanasiadis
When I deleted this line,the code was saved succesfully
PawanKumarPawanKumar
you just change the below.

mail.setToAddresses(new List<String>{iterator.email});
PawanKumarPawanKumar
Please let me know if you still have issue otherwise please mark best answer.