I am new to salesforce. I am practicing triggers. As a part of my learning, I wanted to send an email to the opportunity owner when an Opportunity is modified.

I wrote a trigger on Opportunity and passing trigger.new to the below class.

I believe i have completed the below code except for an error that is popping up. It says

"Method does not exist or incorrect signature: void setToAddresses(User) from the type Messaging.SingleEmailMessage".

Can somebody help me, please?

public class Opportunitytriggerutility {

    public void sendEmail(List<Opportunity> opps){

        string temp_name = 'Opportunity Updated';
        for(Opportunity op: opps){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        EmailTemplate et = [Select Id, Subject, Body from EmailTemplate where DeveloperName = 'Opportunity_Updated'];    
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


  • 2
    you have to use mail.setTargetObjectId(Id) function since you are using EmailTemplate
    – User6670
    Feb 19, 2020 at 18:09
  • Thanks. Will dig out more and understand the use of the function. Feb 20, 2020 at 9:12

1 Answer 1


You need to pass in a list of Ids to the setToAddresses method.

List<Id> IdList = new List<Id>{op.OwnerId};


Please note that you'll need to use ownerId (not owner) to access the Id in trigger.

As noted in comment above, you'll also need to set mail.setTargetObjectId(Id)

  • Thanks. It has helped in resolving the issue. Feb 20, 2020 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.