0

I would like to send an email template ,with merge fields from case, even to a fixed address'[email protected]'.

Template:

Hi, this is case {!Case.CaseNumber}.

Code:

public void sendSingleEmailfromCustomSetting( string TemplateDeveloperName , string ToAddresses){

           EmailTemplate emailTemplate = [select Id, 
                                                 Body  
                                          from EmailTemplate 
                                          where DeveloperName = :TemplateDeveloperName ];

           Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
           email.setToAddresses( toAddresses.split( '[;,]' ) );
           email.setSaveAsActivity(false);
           email.setTargetObjectId(?);
           //Required if using a template
           //Only Users, Contact or Lead allowed for targetObjectId 
           email.setTemplateId(emailTemplate.Id);
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}

The problem is that, in order to send a template, i should set email.setTargetObjectId that is only available for contact, lead, or user.

Is there some workaround?

I would like to use a template, avoiding building message body in apex.

Thanks in advantage for any advice.

2
  • We cannot suggest a workaround unless you provide us with your use case. The email addresses you have in 'ToAddresses', are they fixed?
    – A-d
    Commented Sep 16, 2014 at 14:40
  • Even fixed address'[email protected]'.
    – Enry
    Commented Sep 16, 2014 at 14:41

2 Answers 2

0

Create a dummy contact with the name Dummy and the email address you need. The code below should then allow you to get working from there

public void sendSingleEmailfromCustomSetting( string TemplateDeveloperName , string ToAddresses){
  EmailTemplate emailTemplate = [select Id, Body  from EmailTemplate where DeveloperName = :TemplateDeveloperName ];
  contact con=[Select id from contact Where FirstName = 'Dummy' limit 1];
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setToAddresses( toAddresses.split( '[;,]' ) );
  mail.setSaveAsActivity(false);
  mail.setTargetObjectId(con.Id);
  mail.setSenderDisplayName(‘My Namej’);
  mail.setTemplateId(emailTemplate.id);
  mail.setWhatId(case.id);
  Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
1
  • Thanks a lot! great! for my case was only missing mail.setWhatId(case.id);
    – Enry
    Commented Sep 16, 2014 at 15:07
0

Well for the work around,

Create a Dummy contact with email '[email protected]'. Give its Id to setTargetObjectId(contactId) and give Case's Id to setWhatId(CaseId)

Note : You should avoid populating email address to setToAddresses() since you are using an email Template.

3
  • Thanks a lot for your Help! i guess it ´s better don´t create a contact but getting it with a query
    – Enry
    Commented Sep 16, 2014 at 14:58
  • @Enry No problem, don't forget to upvote the answer too.
    – A-d
    Commented Sep 16, 2014 at 15:01
  • I meant create this contact only once and use its Id everytime you have to send the email. So yes, you can query it or put the Id in code as it is, since you won't be changing it, and its static.
    – A-d
    Commented Sep 16, 2014 at 15:05

You must log in to answer this question.

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