2

We are trying to create an automated case closing process which will scan case description for keywords then based on that pick the correct email template response and send it to customer. All good except most of our cases are B2C and have supplied email but no contact. So when I instantiate a new email object when I set setTemplateId its expecting a contact or lead as the setTargetObjectId. but I only have an email addres..

Is there anyway around this?

1 Answer 1

1

Assuming that you will always have access to at least one contact of the Database, there is a blog post in https://appirio.com/tech-blog/sending-emails-without-targetobject-id-apex that shows a work-around to do so.

Bottom line, it uses a random contact to apply the template on the call of sendEmail, which is not truly sent because it does a rollback. The point is that by that time, the HTML, subject and plain text of the email is already merged, so it can be used to send the real email.

Id whatID = 'xxxxxxxxxxxxxxxxxx';
Contact cnt = [select id, Email from Contact where email != null limit 1];
List<Messaging.SingleEmailMessage> msgList= new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setTemplateId('yyyyyyyyyyyyyyyyyy');
msg.setWhatId(whatID);
msg.setTargetObjectId(cnt.id);
msg.setToAddresses(new List<String>{'[email protected]'});
msgList.add(msg);
// Send the emails in a transaction, then roll it back
Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(msgList); // Dummy email send
Database.rollback(sp); // Email will not send as it is rolled Back
// Send Actual email
List<Messaging.SingleEmailMessage> msgListToBeSend = new List<Messaging.SingleEmailMessage>();
for (Messaging.SingleEmailMessage email : msgList) {
    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
    emailToSend.setToAddresses(email.getToAddresses());
    emailToSend.setPlainTextBody(email.getPlainTextBody());
    emailToSend.setHTMLBody(email.getHTMLBody());
    emailToSend.setSubject(email.getSubject());
    msgListToBeSend.add(emailToSend);
}
Messaging.sendEmail(msgListToBeSend);

Most likely you will have to test this and adapt it a bit to your needs, in case you send multiple emails, etc. But I hope you get the idea behind.

1
  • thanks.. I figured it would have to be something like this.. was hoping there was alternative.
    – SLM3573
    Oct 25, 2017 at 9:35

You must log in to answer this question.

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