0

In my org I've set up an automatic process that sends email messages with the Custom template (with the HTML) to the contact. It has triggered similar code:

OrgWideEmailAddress addr = [select Id from OrgWideEmailAddress where Address = 'info@myorgaddress.com'];
EmailTemplate template = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'CUSTOM_HTML_TEMPLATE'];
List<Messaging.SingleEmailMessage> composedMessages = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(template.Id);
email.setWhatId('5001G00000dZrHh');
email.setOrgWideEmailAddressId(addr.Id);
email.setTargetObjectId('0031G00000rsMgZ');
composedMessages.add(email);
Messaging.sendEmail(composedMessages);

Where WhatId is the Id of the Case, where messages were sent from and TargetObjectId - is the Id of the receiving Contact.

Until 1st of February 2019 both Activities and HTML Email Statuses were created, but then HTML States stopped being inserted. Even though receiver Contact receives the message, and it is listed in a Case Emails, and Contact Activities.

What I've tried:

1) Sending email message to the same contact directly from case manually using VisualForce interface. HTML Status was created with the Activity for the Contact.

2) Sending email message without setWhatId() method. HTML Status was created with the Activity for the Contact. But, obviously, message was sent without filling crucial information from the Case.

3) Tried to add

email.setSaveAsActivity(true);

to the code above, but it didn't help me.

As a last resort, thinking about trying to do this without setWhatId() but with editing substring from the html/text body directly with values from the queried case fields, but it won't be a smart solution.

Thanks in advance!

Typical situation in a Contact page, when executing this method from the console. No HTML Status, but there is a Activity record:

Typical situation in a Contact, when executing this method from the console. No HTML Status, but there is a Activity record

  • are you sure that you have set correct What Id? which organization you are working on? is this developer edition and you have exceeded email allocation limit? – Patlatus Mar 28 at 13:36
  • @Patlatus yes, i am certain, because in a specified Case i see a new Email message being sent with all info needed from the Case field filled out. – RomanK Mar 28 at 13:46
  • That's a Production Org – RomanK Mar 28 at 13:46
  • I tried to implement the same solution and it works for me, what is the code for your template? maybe there is some error in template? did you modify template recently? do you receive any exception when you try to execute this code from anonymous console? – Patlatus Mar 28 at 13:48
  • I am executing this code from anonymous console without any exceptions – RomanK Mar 28 at 13:51
0

I got it done by pre-rendering email message using renderStoredEmailTemplate() method with the caseId specified as whatId (to pre-fill template with the Case info), not sending it but copying the content of rendered email message to the new Messaging.SingleEmailMessage without specified whatId.

That caused sent emails to insert an HTML status, therefore be trackable:

Messaging.SingleEmailMessage renderedEmail = Messaging.renderStoredEmailTemplate(
        getTemplateToDebtor(),
        getOrCreateInvoicingContact(), 
        cs.Id); 

emailToSend.setHtmlBody(renderedEmail.getHtmlBody());
emailToSend.setPlainTextBody(renderedEmail.getPlainTextBody());
emailToSend.setSubject(renderedEmail.getSubject());
emailToSend.setCcAddresses(addCCAddresses());
emailToSend.setBccAddresses(CasePlugIns.ccAddresses(cs));
emailToSend.setFileAttachments(fileAttachments);
emailToSend.setTargetObjectId(getOrCreateInvoicingContact());
emailToSend.setOrgWideEmailAddressId(getOweaId());

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.