Viewer String Helpers
String helpers let you format and manipulate text in Viewer templates. Use these helpers to transform text from Salesforce fieldsβlike names, addresses, emails, and descriptionsβinto properly formatted content for your documents.
Whether you need to normalize casing, trim whitespace, shorten long text, or replace characters, Viewer's string helpers give you precise control over how text appears in generated PDFs and documents.
If you need to display a Salesforce long text field with line breaks preserved, start with nl2br. It is the most important helper for multi-line descriptions, notes, comments, and long text area fields in PDF and Word output.
Function name meaning (quick reference)β
nl2br= preserve line breaks in Salesforce long text and multi-line text fieldslowercase= convert text to lower caseuppercase= convert text to upper casecapitalize= make first letter upper casetitleize= capitalize each wordtrim= remove spaces at start/endtruncate= shorten text to a max lengthreplace= replace part of a string with another valuestartsWith= check if text starts with a valueendsWith= check if text ends with a value
nl2brβ
Converts newline characters to format-specific break tags so Salesforce long text fields keep their line breaks in generated documents.
What it does:
- Keeps the line breaks that users entered in Salesforce
- Works for both PDF and Word output
- Helps long text fields display more naturally in the final document
When to use: Display multi-line Salesforce fields like Description, Notes, Comments, or Long Text Area fields without losing line breaks.
Scenario: Display a Salesforce long text field properly in both PDF and Word output.
Expected result:
- PDF format: The text keeps the same line breaks entered in Salesforce
- Word format: The text also keeps the same line breaks entered in Salesforce
Use nl2br whenever you display a Salesforce long text field that includes line breaks and you want the document output to match what users entered in Salesforce.
lowercaseβ
Converts all text to lowercase letters.
Scenario: Normalize email display or make text consistent.
Expected result: john.doe@company.com
uppercaseβ
Converts all text to uppercase letters.
Scenario: Highlight reference codes or create bold headings.
Expected result: CUSTOMER REF-12345
capitalizeβ
Capitalizes only the first character, leaving the rest unchanged.
Scenario: Make stage labels or field values easier to read.
Expected result: Qualification stage
titleizeβ
Capitalizes the first letter of each word (title case).
Scenario: Format product names, categories, or headings.
Expected result: Enterprise Software Solutions
trimβ
Removes whitespace (spaces, tabs, newlines) from the beginning and end of text.
Scenario: Clean data imported from external systems or user input.
Expected result: Acme Corporation
Always use trim on fields that might contain extra whitespace from imports, integrations, or manual data entry.
truncateβ
Shortens text to a maximum length. Adds "..." at the end if text is cut.
Syntax: {{truncate value length}}
Scenario: Keep long descriptions compact in tables or summaries.
Expected result: This is a very long product...
The length parameter includes the "..." characters. Choose lengths like 50, 80, or 120 based on your document layout.
replaceβ
Replaces all occurrences of one string with another.
Syntax: {{replace value "find" "replaceWith"}}
Scenario: Format IDs, clean data, or substitute characters.
Expected result: Ref-NUM-12345
- Convert underscores to hyphens or spaces
- Remove unwanted characters
- Standardize formatting across different data sources
startsWithβ
Checks if text starts with a specific string. Use with conditional blocks.
Syntax: {{#startsWith value "prefix"}}...{{/startsWith}}
Scenario: Show conditional content based on prefixes or codes.
Expected result: This is an enterprise account (content displays because string starts with "ENT-")
endsWithβ
Checks if text ends with a specific string. Use with conditional blocks.
Syntax: {{#endsWith value "suffix"}}...{{/endsWith}}
Scenario: Show conditional content based on file extensions or suffixes.
Expected result: This is a PDF file (content displays because string ends with ".pdf")
Combining String Helpersβ
You can combine helpers for more complex transformations.
When combining helpers, inner helpers execute first. For example:
{{uppercase (trim " hello ")}} β trims first β then converts to uppercase β HELLO
Document Formatting Helpersβ
These helpers format text output for document rendering in PDF and Word formats.
preserveLineBreaksβ
Preserves line breaks in text using Word XML formatting (always Word format).
From implementation behavior:
- Converts all newline characters to Word XML
<w:br/>sequences - Useful when you always output to Word documents
- Simpler than
nl2brsince Word format is implicit
When to use: Format text for Word document output where consistent line-break preservation is needed.
Scenario: Display account notes with proper line breaks in Word documents.
Expected result: Line breaks preserved in Word XML format
Common Use Casesβ
How do I format names consistently?β
Use titleize to capitalize each word in a name:
For all caps: {{uppercase Contact.Name}}
How do I clean up imported data with extra spaces?β
Use trim to remove leading and trailing whitespace:
For fields that might have inconsistent spacing, always apply trim before other formatting.
How do I shorten long descriptions for tables?β
Use truncate with an appropriate length:
Choose truncation lengths based on your layout:
- Short labels: 30-50 characters
- Table cells: 80-100 characters
- Summaries: 120-150 characters
How do I replace characters in field values?β
Use replace to substitute one string for another:
Common uses:
- Convert underscores to hyphens:
{{replace value "_" "-"}} - Remove parentheses:
{{replace value "(" ""}}and{{replace value ")" ""}} - Standardize separators:
{{replace value "/" "-"}}
How do I show content conditionally based on text patterns?β
Use startsWith or endsWith with conditional blocks:
How do I format multi-line descriptions for PDF output?β
Use nl2br helper with "PDF" format:
Result: Newlines become <br/> HTML tags
How do I format multi-line text for Word documents?β
Use nl2br helper with "Word" format:
Result: Newlines become Word XML </w:t><w:br/><w:t> sequences
How do I format notes while avoiding undefined field errors?β
Combine nl2br with isTruthy to handle missing data:
How do I handle special characters (ampersands) in formatted text?β
The nl2br helper automatically escapes ampersands (& β &) to prevent XML issues:
Result: Ampersands properly escaped, newlines converted.
Best Practicesβ
- Use one helper per line for clarityβnesting is powerful but can be hard to debug
- Apply helpers to direct fields (e.g.,
Account.Name) rather than complex expressions - Choose appropriate truncation lengths based on your document layout (50, 80, 120 chars)
- Always test with real data to ensure formatting works with edge cases
- Combine with trim when working with imported or user-entered data