In Salesforce, custom automated processes often need to generate formatted summaries—such as tables, styled headings, or colored lists—directly inside a Rich Text Area field. Populating rich text fields via Apex code allows you to store pre-formatted HTML data directly on a record. This formatted content can then be reused seamlessly inside standard Salesforce email templates without needing complex template code.
1. Constructing Styled Lists and Headings in Apex
You can dynamically build structured lists, format labels in bold, and add inline inline CSS styling (like text colors and line heights) inside an Apex string before assigning it to your rich text field.
This Apex snippet builds a custom heading and an ordered list with custom styling:
// Define heading with inline style
String body = '<h3 style="color: #2e6c80;">Summary Details:</h3>\n';
body += '<ol style="list-style: none; font-size: 12px; line-height: 32px;">\n';
// Replace specific characters (e.g., semicolons with commas) and format values
String labelName = 'Category Options';
String rawValues = 'Option A; Option B; Option C';
String formattedValue = rawValues.replaceAll(';', ' , ');
body += '<li style="clear: both;"><b>' + labelName + ' : </b>' + formattedValue + '</li>';
body += '</ol>';
// Assign the HTML string directly to the Rich Text field
accountRecord.Custom_Rich_Text_Field__c = body;
2. Generating Dynamic HTML Tables via Helper Methods
Creating HTML tables programmatically in Apex is ideal for displaying multi-row field data or line items cleanly. Using modular helper methods keeps your table generation clean and readable.
Use a helper function to append individual table rows (
<tr> and <td>) dynamically:
public class RichTextTableBuilder {
public static void updateRichTextWithTable(Account acc) {
String table = '<p><strong style="color: #2e6c80;">Account Summary Table</strong></p>\r\n';
table += '<table border="1" cellspacing="0" cellpadding="5" style="font-size:12px; border-collapse: collapse;">\r\n';
table += '<tbody>';
// Add dynamic table rows using helper method
table += createColumn('Account Name', acc.Name);
table += createColumn('Industry', acc.Industry);
table += createColumn('Phone Number', acc.Phone);
table += '</tbody>\r\n</table>';
// Assign formatted HTML table to the Rich Text Area field
acc.Custom_Rich_Text_Field__c = table;
}
// Helper method to format table rows
public static String createColumn(String columnName, String columnValue) {
String safeValue = String.isNotBlank(columnValue) ? columnValue : 'N/A';
return '<tr>\r\n<td style="font-weight: bold; padding: 4px;">' + columnName +
'</td>\r\n<td style="padding: 4px;">' + safeValue + '</td>\r\n</tr>';
}
}
- Salesforce HTML Sanitization: Salesforce automatically strips unsupported HTML tags (like
<script>,<iframe>, or external style links) when saving to Rich Text fields. Stick to inline CSS styles (style="...") and standard structural markup (<table>,<p>,<b>,<span>). - Field Length Limitations: Rich Text Area fields have a character limit (up to 131,072 characters including hidden HTML tags). Excessive inline styling can rapidly increase character count, so keep tag attributes concise.
- Unescaped Null Values: Always validate strings before appending them to HTML blocks to prevent
nulltext from displaying inside formatted tables.
- Target Field Type: Rich Text Area (
Html) - Max Field Capacity: Up to 131,072 characters
- Supported Markup: Standard HTML tags (
<p>,<table>,<ul>,<span>) with inline CSS styling - Primary Benefit: Direct inclusion in standard HTML/Lightning Email Templates