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
LaurenP6777LaurenP6777 

SingleEmailMessage to Opportunity Owners

Hi Everyone, 

I have been trying to create an Apex Controller that will allow users to click a "Send Email" button and have an email alert fire to the current opportunity owner AND all child opportunity owners. I keep getting the following error:

Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setToAddresses(List<Opportunity>) at line 25 column 4

Can someone explain how I can change the email addresses in my query to a list of String emails?
Public with sharing class Sendemail{

   public Opportunity Opp;
   
   public LIst<Opportunity> Oppty;
   
   public Sendemail(Apexpages.StandardController controller) {
   this.Opp=(Opportunity)controller.getRecord();
   
   }
 
  
 Public void sendEmailFunction(){
 
  
   List<Opportunity> Owners =[Select Owner.Email FROM Opportunity where Id=:opp.id OR Parent_Opportunity__r.Id=:opp.Id];   
   
    
  

   
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  
  
   mail.setToAddresses(Owners);
  


   mail.setReplyTo('myemail@mail.com');
   mail.setSenderDisplayName('My Name');
  mail.setPlainTextBody('A related Opportunity has been won');
  mail.setHtmlBody('A related Opportunity has been won');
   mail.setBccSender(false);
   mail.setUseSignature(false);
  
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 } 

}

Thanks!
Best Answer chosen by LaurenP6777
LaurenP6777LaurenP6777
I was able to convert this to a string on my own... this is my new code if anyone is interested. It sends an email to the current opportunity owner and any child opportunity owners:
 
Public with sharing class Sendemail{

   public Opportunity Opp;
   
   public LIst<Opportunity> Oppty;
   
   public Sendemail(Apexpages.StandardController controller) {
   this.Opp=(Opportunity)controller.getRecord();
   
   }
 
  
 Public void sendEmailFunction(){
 
  
   List<Opportunity> Owners =[Select Owner.Email FROM Opportunity where Id=:opp.id OR Parent_Opportunity__r.Id=:opp.Id];   
   
  List<String> newlist = new List<String>();
   for(Opportunity Vopp:Owners)
   {newlist.add(vopp.owner.email);
   }  
  

   
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  
  
   mail.setToAddresses(Owners);
  


   mail.setReplyTo('myemail@mail.com');
   mail.setSenderDisplayName('My Name');
  mail.setPlainTextBody('A related Opportunity has been won');
  mail.setHtmlBody('A related Opportunity has been won');
   mail.setBccSender(false);
   mail.setUseSignature(false);
  
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 } 

}

 

All Answers

MagulanDuraipandianMagulanDuraipandian

Hi,
Add the owners emails to set<String> and pass it to setToAddresses().

www.infallibletechie.com
LaurenP6777LaurenP6777
When I try to turn the Email addresses into a string list, I get an "Incompatible Element Type Opportunity for List of STring" error. Can you someone actually show me who they would do this?
siddarth rajsiddarth raj

It accepts only String collection of array..and not more than 100

 A list of email addresses to which you are sending the email. The maximum number of email addresses allowed is 100. This argument is allowed only when a template is not used. 

LaurenP6777LaurenP6777
Ok, so there is no way to do this? Send an alert to a list of Opportunity Owners? I don't need a template, just a way to make my Opportunity Owner Emails list be a String. There will never be more than 25
LaurenP6777LaurenP6777
I was able to convert this to a string on my own... this is my new code if anyone is interested. It sends an email to the current opportunity owner and any child opportunity owners:
 
Public with sharing class Sendemail{

   public Opportunity Opp;
   
   public LIst<Opportunity> Oppty;
   
   public Sendemail(Apexpages.StandardController controller) {
   this.Opp=(Opportunity)controller.getRecord();
   
   }
 
  
 Public void sendEmailFunction(){
 
  
   List<Opportunity> Owners =[Select Owner.Email FROM Opportunity where Id=:opp.id OR Parent_Opportunity__r.Id=:opp.Id];   
   
  List<String> newlist = new List<String>();
   for(Opportunity Vopp:Owners)
   {newlist.add(vopp.owner.email);
   }  
  

   
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  
  
   mail.setToAddresses(Owners);
  


   mail.setReplyTo('myemail@mail.com');
   mail.setSenderDisplayName('My Name');
  mail.setPlainTextBody('A related Opportunity has been won');
  mail.setHtmlBody('A related Opportunity has been won');
   mail.setBccSender(false);
   mail.setUseSignature(false);
  
   //mail.setHtmlBody('<b> This is HTML body </b>' );
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
 } 

}

 
This was selected as the best answer