Assembling HTML Letterhead Emails in APEX?

Assembling HTML Letterhead Emails in APEX?

On May 15, 2025, Posted by , In Apex,Salesforce Technical Questions, With Comments Off on Assembling HTML Letterhead Emails in APEX?
Assembling HTML Letterhead Emails in APEX

Question:

How can I merge HTML from an EmailTemplate and BrandTemplate into a single, fully-assembled HTML document using APEX?

I am working on an email service integration project and need to combine the contents of two Salesforce objects:

  1. BrandTemplate.value: This contains broken HTML (styles not associated with tags) and is wrapped in CDATA tags that hold the letterhead structure.
  2. EmailTemplate.HTMLValue: This contains user-created HTML email content.

Salesforce does not natively support CDATA manipulation, and my attempts to parse both fields into XML or strip them into plain text for manual concatenation have failed. I don’t need to handle merge fields or codes; I only want the resulting document to include the combined HTML from these two fields.

What approaches can I use to achieve this goal effectively?

Answer:

One unconventional but effective solution involves leveraging Salesforce’s built-in email generation mechanism to merge and parse the HTML from BrandTemplate and EmailTemplate. Here’s how it works:

CRS Info Solutions is one of the premier institutes offering Salesforce training in Seattle. Enroll for free demo today.!!

First, create a SingleEmailMessage in APEX using the desired email template:

// Reserve email capacity
Messaging.reserveSingleEmailCapacity(1);

// Create the email message
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'invalid@emailaddr.es'}; // Use an invalid email address
mail.setToAddresses(toAddresses);
mail.setUseSignature(false);
mail.setSaveAsActivity(false);
mail.setSenderDisplayName('Example Sender');
mail.setTargetObjectId(UserInfo.getUserId());
mail.setTemplateId(sendTemplate.Id); // Set the template ID

Next, “send” the email by invoking the Messaging.sendEmail method. To prevent the email from actually being sent, wrap the operation in a transaction and roll it back:

Savepoint sp = Database.setSavepoint();
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
Database.rollback(sp);

Even though the email isn’t sent due to the rollback, Salesforce processes the email template and merges the BrandTemplate and EmailTemplate. The fully assembled HTML and text content are accessible in the following variables:

String mailTextBody = mail.getPlainTextBody(); // Plain text version
String mailHtmlBody = mail.getHTMLBody();     // Full HTML version
String mailSubject = mail.getSubject();       // Merged subject line

This approach takes advantage of Salesforce’s internal parsing and merging mechanism, bypassing the need to manually handle CDATA tags or other complexities.

Alternative Consideration:

If you prefer not to use this “hack,” you can attempt manual parsing by leveraging an external library or service to handle CDATA tags and integrate the content programmatically. However, this can be error-prone and time-intensive compared to the built-in merging functionality Salesforce provides.

The above solution may not align perfectly with standard practices, but it delivers a reliable and practical way to achieve the desired outcome in scenarios where built-in merging functions are not directly accessible.

Accelerate Your Salesforce Career in Seattle

Elevate your Salesforce skills with expert-led  Salesforce online training  in Seattle, tailored for both beginners and professionals. Gain practical experience through real-world projects and master cloud-based CRM solutions with guidance from certified instructors. Unlock exciting career opportunities and become a Salesforce expert with comprehensive, hands-on learning at CRS Info Solutions.

CRS Info Solutions is a leading Salesforce training in Seattle, offering a thorough, real-time project-based curriculum. Our courses cover Salesforce Admin, Developer, Integration, Marketing Cloud, CPQ, and Lightning Web Components (LWC). With expert instructors offering hands-on training, we ensure you’re well-prepared for real-world challenges. Join us now to become a certified Salesforce professional and take the next step in your career.

Enroll today for a free demo at CRS Info Solutions, Seattle!!!

Comments are closed.