Dev: Generate HTML String
Published on February 10, 2026
DRTE
Build Document and Return HTML String (generateHTMLService)
The generateHTMLService method (Build Document and return html string) invokes the core DRTE assembly engine to merge Salesforce data into a template and returns the result as a raw HTML String.
| Stateless by design. Unlike generateDocumentService (Build Document and return Preview Id), which saves a Preview Record and returns its ID, this method creates no record. It returns the output immediately as a string. |
1. Functional Summary
Because the merged document is returned as a string, this method is ideal for:
- Populating the body of an Email Alert.
- Sending document content to an external API or middleware.
- Displaying content in a standard Flow "Display Text" component.
2. Input Parameters
| Parameter | API Name | Type | Required | Description |
| Parent Record ID | parentRecordId | ID | Yes | The ID of the record used as the data context (e.g., Account, Opportunity). |
| Template ID | documentTemplateId | ID | Yes | The ID of the pscdnyrichtext__PSC_Document_Template__c (DRTE) record to be processed. |
3. Output Values
| Parameter | Data Type | Description |
| HTML Content | Text (String) | The complete, merged HTML string generated from all active template sections. |
4. Architectural Comparison: Preview ID vs. Raw HTML
Choosing the right method is critical for performance and scalability:
| Feature | Return Preview ID | Return HTML String |
| Persistence | Creates a temporary record in the database. | No record created; string exists only in memory. |
| UI Interaction | Best for use with the DRTE Editor LWC. | Best for automated emails or external calls. |
| Data Size | Handles very large documents easily. | Subject to Salesforce Flow string length limits. |
| Cleanup | Requires eventual deletion of Preview record. | No cleanup required. |
5. Flow Implementation Guide
Scenario: Automated "Thank You" email with an order summary.
- Trigger: A Flow runs when an Order is "Activated."
- Add Action: Search for "Build Document and return html string".
- Map Variables:
- parentRecordId: {!$Record.Id}
- documentTemplateId: Your specific Summary Template ID.
- Store Result: Assign the output to a text variable, e.g., varEmailBody.
- Send Email: Use the standard Send Email action in Flow. Set the body to {!varEmailBody} and ensure Rich Text Templating is enabled on the email action.
| Result: When the Order is activated, the customer receives an email whose body is the fully merged template, with no Preview record to clean up. |
6. Developer Considerations & Limits
| String Limits | Salesforce Flow variables can hold large amounts of text, but be cautious of the 32,768 character limit for certain standard "Long Text Area" fields if you plan on mapping this string back to a field. For extremely large documents (e.g., 50+ page legal contracts), the Preview ID method is safer. |
| Bulkification | This method processes the first item in the input list (Ids[0]). When using it in a loop within a Flow, ensure you do not hit the SOQL limit, as each call triggers a query for the template sections. |
| CSS Styles | Any CSS defined in the template sections is returned inline or within style tags in the string. Some email clients (like Outlook) have limited support for advanced CSS. |
Knowledge Article Number: KA-00105