Skip to main content

Populate email body on custom CKEDITOR email editor in Salesforce.com

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

Popular posts from this blog

Einstein Bot user authentication

Using Bot for data manipulation use case in your company requires need of implementing some extra security layer to make it spoof proof. One of exciting salesforce feature we have is the well known Einstein Bot which many companies have implemented. I am going to cover detail step-by-step implementation of User validation use case using encrypted token. STEP-1: Create a Site & Site User go to setup > Sites & Domains > Sites Create a Site and make your user as "Site Contact". This is a prerequisites for live agent or Embedded Service setup. STEP-2 : Create a Embedded Service(formerly snap-ins) go to Service setup > Embedded Service Create a new Embedded Service Deployment record and copy the embedded service code snipped generated in a notepad. STEP-3  : Create a Visualforce page to test the chatbot (it will simulate the actual web portal which is going to consume the embedded service snipped.) BotController.apxc public class BotControlle...

Dynamically populate lightning:combobox

In order to dynamically populate values in lightning:combobox using javascript we can create a list variable and push the values. We also require the values like ids associated to the particular option for back-end logic. For example  : If we want to show the list of contacts using lightning:combobox component where the option should display the contact name but the value should be the contact id which will be used for back-end logic For this we can create a temporary Map variable in our lightning component and set the value and label parameters of the lightning:combobox. Here is the code for this component Combo.app <aura:application extends="force:slds" access="global" description="Lightning Combo Demo"> <c:Combo_Cmpt/> </aura:application> Combo_Cmpt.cmp <aura:component implements="flexipage:availableForAllPageTypes" access="global" controller="ComboController"> <!-- ...

Use of wrapper class in lightning:datatable

As you guys know the wrapper class concept in visualforce pages , we generally use it to create a data-table which fetches data from different objects or if we want to redirect user to some other page on click of a link as one of the column of data-table.        For example we want a column "Account Name" on the data-table which is a link and once user clicks it should redirect respective account record. Or , suppose we want to display a column with some images or icons as a visual indicator Or what not. These requirements require us to use a wrapper on the lighting data-table (lightning:datatable) I am going to use my previous account search example ( Account Search Lightning Component ) and explain the use of wrapper. AccountSearchWrapper.app <aura:application extends="force:slds" access="global" >     <c:AccountSearchWrapper /> </aura:application> AccountSearchWrapper.cmp <aura:component controller="...