+ Start a Discussion
Dilip KulkarniDilip Kulkarni 

email class issue

Hi,
I am getting error:  ' Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setToAddresses(String)'  in my email class which is as below:

 List<Messaging.SingleEmailMessage> sme = new List<Messaging.SingleEmailMessage>();
     for(case c: newList){
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
      email.setTemplateId('00X500000013avG');
      email.setToAddresses(insideSalesEmailMap.get(c.accountId));
      email.targetObjectId(c.id);
      email.setSaveAsActivity(false);
      sme.add(email);
     }

Please help with the same.
Dilip_VDilip_V
Hi Dilip,

You have to pass list of addresses to the setToAddresses method not a string.
setToAddresses(toAddresses)

Optional. A list of email addresses or object IDs of the contacts, leads, and users you’re sending the email to. The maximum number of email addresses allowed is 100.
Signature

public Void setToAddresses(String[] toAddresses)

Parameters

toAddresses
Type: String[]
Return Value
I think 'insideSalesEmailMap.get(c.accountId)' is returning string.Thats why you are gettung compile error. 

For more info:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm​ (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm)


Let us know if it works.
Mark it as best answer if it works.

Thanks.
Dilip KulkarniDilip Kulkarni
Hi Dilip,
Thanks for the response. Here I want to send mail to inside sales rep of the related account when case is opened(not to other reps).
Much appreciated if you can give me corrected class.
Dilip_VDilip_V
Dilip,

Please change the map definition from Map<String,String> or Map<Id,String> to Map<String,List<String>>.
PLease share apex class so that I can see the map definition/declaration.

Let us know if it helps.
Mark it as best answer if it works.
Thanks.
Dilip KulkarniDilip Kulkarni
Hi,
Below is my full handler class.Please check and correct it.

public class CaseTriggerHandler{
 
    public static void afterInsert(List<case> newList){
     list<Account> accList = new list<Account>();
     for(case c: newList){
      if(c.account!= null)
       accList.add(c.account);
     }

     //query email address from account object.
        Map<id,String> insideSalesEmailMap = new Map<Id, String>();
     for(Account acc: [select id,thermage_tlr__Rep_2__c from Account where id IN :accList]){
            insideSalesEmailMap.put(acc.id, acc.thermage_tlr__Rep_2__c);
        }
     
     // preparing for Email send out. 
     List<Messaging.SingleEmailMessage> sme = new List<Messaging.SingleEmailMessage>();
     for(case c: newList){
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
      email.setTemplateId('00X500000013avG');
      email.setToAddresses(insideSalesEmailMap.get(c.accountId));
      email.targetObjectId(c.id);
      email.setSaveAsActivity(false);
      sme.add(email);
     }

     //send Email
     if(sme != null && sme.size() > 0)
      Messaging.sendEmail(sme);
  }
}
 
Dilip_VDilip_V
Dilip,

Try this class.
public class CaseTriggerHandler{
 
    public static void afterInsert(List<case> newList){
     list<Account> accList = new list<Account>();
     for(case c: newList){
      if(c.account!= null)
       accList.add(c.account);
     }

     //query email address from account object.
        Map<id,List<String>> insideSalesEmailMap = new Map<Id, List<String>>();
     for(Account acc: [select id,thermage_tlr__Rep_2__c from Account where id IN :accList]){
	        List<String> emailAddresses = new List<String>();emailAddresses.add('acc.thermage_tlr__Rep_2__c');
            insideSalesEmailMap.put(acc.id, emailAddresses);
        }
     
     // preparing for Email send out. 
     List<Messaging.SingleEmailMessage> sme = new List<Messaging.SingleEmailMessage>();
     for(case c: newList){
      Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
      email.setTemplateId('00X500000013avG');
      email.setToAddresses(insideSalesEmailMap.get(c.accountId));
      email.targetObjectId(c.id);
      email.setSaveAsActivity(false);
      sme.add(email);
     }

     //send Email
     if(sme != null && sme.size() > 0)
      Messaging.sendEmail(sme);
  }
}

Let us know if it helps.

Mark it as best answer if it works.

Thanks