1

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'];    
        mail.setToAddresses(op.Owner);
        mail.setTemplateID(et.Id);
        mail.setSubject(et.Subject);
        mail.setHtmlBody(et.Body);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }


    }

}
2
  • 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

0

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

List<Id> IdList = new List<Id>{op.OwnerId};
mail.setToAddresses(IdList);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.html

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)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .