0

I would like to send an email template ,with merge fields from case, even to a fixed address'test@email.com'.

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.

  • 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 Sep 16 '14 at 14:40
  • Even fixed address'test@email.com'. – Enry Sep 16 '14 at 14:41
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 });
}
  • Thanks a lot! great! for my case was only missing mail.setWhatId(case.id); – Enry Sep 16 '14 at 15:07
0

Well for the work around,

Create a Dummy contact with email 'test@email.com'. 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.

  • Thanks a lot for your Help! i guess it ´s better don´t create a contact but getting it with a query – Enry Sep 16 '14 at 14:58
  • @Enry No problem, don't forget to upvote the answer too. – A-d Sep 16 '14 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 Sep 16 '14 at 15:05

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.