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
    you have to use mail.setTargetObjectId(Id) function since you are using EmailTemplate – User6670 Feb 19 at 18:09
  • Thanks. Will dig out more and understand the use of the function. – vamsi krishna Feb 20 at 9:12
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)

| improve this answer | |
  • Thanks. It has helped in resolving the issue. – vamsi krishna Feb 20 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.