Other than using out-of-the-box Salesforce email editor , there are some scenarios where you need to build your custom email editor. You can build a custom email editor using CKEDITOR HTML editor.
Once you build the custom editor , you will face the issue while fetching the body of the template.
To display the email body in a HTML format from the predefined email templates , you can send the email using apex and then rollback immediately and fetch the email body in correct HTML format.
Here is the sample code :-
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String toAddress = 'testemaildomain@test.com';
String templateId = '<Fetch Id of email template>'
// Set the template id for which we need the HTML formatted body.
email.setTemplateId(templateId);
email.setWhatId(<Object Id>);
email.setSaveAsActivity(false);
email.setTargetObjectId(contactId);
email.setToAddresses(new String[] {toAddress });
if(!Test.isRunningTest()){
Savepoint savept = Database.setSavepoint();
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
Database.rollback(savept);
}
// Use the email instance to fetch the body
EmailTemplate emailTemlt = [Select id , body , TemplateType from EmailTemplate where id=: templateId];
String htmlEmailBody ='';
if(emailTemlt.TemplateType=='html' || emailTemlt.TemplateType=='visualforce' || emailTemlt.TemplateType=='Custom' ) {
htmlEmailBody = email.getHTMLBody();
}else if(templateObj.TemplateType=='Text') {
htmlEmailBody = email.getPlainTextBody();
}
Comments
Post a Comment