Skip to main content

How to Insert HTML Tables and Formatted Data into Salesforce Rich Text Fields via Apex

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.

In plain words: You can construct raw HTML strings in your Apex code and assign them directly to a Rich Text Area field on any record. When saved, Salesforce renders the underlying HTML as styled text, tables, and lists in the UI and email merge fields.

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.

Real-Life Example: Generating a Styled Section
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.

Real-Life Example: Programmatic Table Construction
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>';
    }
}
Common Developer Pitfalls:
  • 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 null text from displaying inside formatted tables.
Rich Text Area fields preserve raw HTML rendering inside standard Salesforce email template merge fields without requiring custom Visualforce template logic.
360 Summary Card
  • 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