I have a VF email template as below which has relatedTOType as Job_Application__c and recipientType as User. I am using some merge field in this template:
<messaging:emailTemplate subject="TEST" recipientType="User" relatedToType="Job_Application__c">
<messaging:htmlEmailBody >
<html><body>
Hello {!relatedTo.Job_Opening__r.Hiring_Manager__r.Name},
<br/><br/>
Project : {!relatedTo.Job_Opening__r.Account__r.Name}<br/>
SO: {!relatedTo.Job_Opening__r.SO__c}<br/>
Candidate Name : {!relatedTo.Candidate__r.Name}<br/>
Candidate Phone: {!relatedTo.Candidate__r.Mobile__c}<br/>
Candidate Email: {!relatedTo.Candidate__r.Email__c}<br/>
<br/><br/>
Please <a href="mailto:[email protected]?Subject=Interview Assign {!relatedTo.Id}&body=Enter Employee ID :" target="_top">Reply with Employee Id</a> to Assign an interviewer
or click <a href="mailto:[email protected]?Subject=Interview Assign {!relatedTo.Id}&body=Smart Assign" target="_top">Smart Assign</a> to automatically assign an interviewer <br/>
<br/>
Thank You<br/>
Candid Application Admin
</body></html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
When I use a singleemailmessaging class to send an email I get an email with empty body. I researched a bit to find out the I need to mention whatID to be used as relatedToType object id. I had to comment- setTargetObjectId as I do not have contacts in my org(My org does not have Sales cloud) and setSaveAsActivity as false. Doing this I get a blank email body. Part of my code which send email:
EmailTemplate et = [Select Id,Name from EmailTemplate where Name = 'The VF email template name'];
Contact cnt = [select id, Email from Contact where email != null limit 1];
mail.setTemplateId(et.Id);
mail.setWhatId(jobapp.id);
mail.setTargetObjectId(cnt.id);
//mail.setSaveAsActivity(false);
mail.setToAddresses( toAddresses );
mail.setOrgWideEmailAddressId(owea.get(0).Id);
mail.setSubject(Candidate.Name +' '+'has been assigned to the project'+' '+'"'+jobopenEmail.Account_Name__c+'"' +' '+'('+' '+'SO#'+' '+jobopenEmail.SO__c +')' + ' '
+' '+jobapp.id);
Savepoint sp = Database.setSavepoint();
Messaging.SendEmailResult [] r =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
Database.rollback(sp);
Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
emailToSend.setToAddresses(mail.getToAddresses());
//emailToSend.setPlainTextBody(mail.getPlainTextBody());
emailToSend.setHTMLBody(mail.getHTMLBody());
emailToSend.setSubject(mail.getSubject());
Messaging.SendEmailResult [] sndmail =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {emailToSend});
I think my solution should be here - https://appirio.com/tech-blog/sending-emails-without-targetobject-id-apex Any suggestion what I might be missing here?
setTargetObjectId
then surrounding the send with a rollback (so the email doesn't send), so he can then use the body that was generated by the template. But never actually sent to the dummy contact. Pretty slick. Granted I didn't actually try it, but it makes sense. I'm sure you will have to tweak up his example a bit.Savepoint
then use the variable inside the rollback. If the answer works let me know and I will whip up a clean solution for this thread that is a bit easier to read than the blog post.WhatId
.