Dev: Generate HTML String
1. Functional Summary
The generateHTMLService (Build Document and return html string) method invokes the core DRTE assembly engine to merge Salesforce data into a template, returning the result as a raw HTML String.
Unlike the generateDocumentService (Build Document and return Preview Id)(which saves a Preview Record and returns an ID), this method is stateless. It provides the immediate string output, making it 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 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.
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 this 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 will be returned inline or within style tags in the string. Note that some email clients (like Outlook) have limited support for advanced CSS.