---
sourceDocument: Australia Employee Service Management
sourceDocumentLink: https://www.servicenow.com/docs/r/employee-service-management

 Release :

    - australia

ft:locale :

    - en-US

ft:publication_title :

    - Australia Employee Service Management

ft:clusterId :

    - emplsm

bundleId :

    - emplsm

workflow :

    - Employee


---

# Document template scripts

# Document template scripts {#ariaid-title1}

Release version: Australia  
Updated March 12, 2026  
![](https://www.servicenow.com/docs/portal-asset/ico-clock) 3 minutes to read
Summarize  
![AI sparkle icon](https://servicenow.com/docs/portal-asset/ai-sparkle-icon) Summarized using AI  
This content was generated using new OpenAI-powered functionality. Results are provided on an as is basis and are not guaranteed to be accurate or complete.  

## Summary of Document Template Scripts

Document template scripts in ServiceNow enable dynamic modification of the text within the body of HTML document templates.
These scripts can perform a range of functions from simple displays of HR data to complex database queries.
By embedding a script tag in the format${templatescript:scriptname}within an HTML template, you can reuse scripts across multiple document templates efficiently.
Show full answer Show less  
Scripts are created and managed under **Document Templates \> Document Templates Script**. When the HTML template's Sanitize option is enabled, script outputs are automatically sanitized for security.

## How Document Template Scripts Work

A script typically receives a `target` GlideRecord representing the task record and optionally the `docTemplate` record, which provides context such as the selected language and date format of the document template. The script constructs HTML content dynamically by querying related data, formatting it into tables or other HTML elements, and returns this content to be rendered within the final document.

## Key Features

* **Dynamic Data Integration:** Scripts can query ServiceNow tables (e.g., HR contacts) and incorporate live data into documents.
* **Localization Support:** Using APIs like `getDisplayValueLang`, scripts can translate dynamic tokens according to the language selected in the document template, ensuring content is localized for end users.
* **Date Formatting:** The `getByFormat` API allows dates to be displayed in the template's specified format, ensuring consistency with regional preferences.
* **Reusable Scripts:** Scripts can be invoked from multiple templates via embedded tags, promoting efficiency and maintainability.
* **Secure Output:** Optional sanitization of script output prevents security risks when rendering HTML.

## Practical Application Example

An example script named `employeeemergencycontacts` queries the `snhrcorecontact` table to retrieve emergency contact details for an employee referenced in a task record. It generates an HTML table displaying contact name, mobile phone, relationship to the employee, priority, and date of birth. This table is then embedded in an Employee Profile document template by placing `${templatescript:employeeemergencycontacts}` in the template body.

When the document is generated, the script respects the document template's language and date format settings, translating fields like "priority" and "relationship" and formatting dates accordingly. For example, if the template language is set to German and the date format to `dd/MM/yyyy`, these values appear localized in the final generated document.

## Benefits for ServiceNow Customers

* Create highly customized and dynamic documents that reflect live data and respect localization settings.
* Reuse template scripts across different document templates to streamline document generation workflows.
* Ensure consistency in data presentation aligned with organizational language and formatting standards.
* Enhance document security by enabling output sanitization.  
With document template scripts, you can dynamically change the text in the body of the
HTML template. Document template scripts allow you to perform simple tasks, such as displaying
HR data, and complex ones, such as making advanced database queries.
You can add a `${template_script:script name}` embedded script tag to the body of the HTML template, replacing script name with the name of the script you created. This makes it easy to use the same scripts in multiple document templates. You can create a script by navigating toDocument TemplatesDocument Templates Script.  
Note:  
The output of the HTML script is automatically sanitized when the Sanitize option is enabled in the HTML template. For more details, refer to the Sanitize field in [Configure an HTML document template](https://www.servicenow.com/docs/17YHRcf8VKNdHWo8EZPIVw "Create or modify the document template with your unique company logo and audience criteria.").

## Example of how to create and use a document template script in an HTML template {#document-template-scripts__section_ffw_ftq_klb}

1. The employee_emergency_contacts script populates the emergency contacts list in an Employee Profile document.  

       (function runTemplateScript(target /*GlideRecord for target task*/ ) {
       	var getHeaderCell = function(label) {
       		return '<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">' + label + '</th>';
       	};	
       	var getDataCell = function(value) {
       		return '<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">' + value + '</td>';
       	};
       	
       	var html = '';
       	var hrTaskGr = new GlideRecord('sn_hr_core_contact');
       	hrTaskGr.addQuery('user', target.getValue('subject_person'));
       	hrTaskGr.query();
       	while(hrTaskGr.next()) {
       		html = html + '<tr>';
       		html = html + getDataCell(hrTaskGr.getDisplayValue('name'));
       		html = html + getDataCell(hrTaskGr.getDisplayValue('mobile_phone'));
       		html = html + getDataCell(hrTaskGr.getDisplayValue('relation_to_employee'));
       		html = html + '</tr>';
       	}
       	
       	if(!gs.nil(html))
       		html = '<h4>Emergency Contact Information</h4><table width="500px;"><tr>' + getHeaderCell('Name') + getHeaderCell('Mobile phone') + getHeaderCell('Relationship') + html + '</table>';
       	
       	return html;
       })(target);

2. The employee_emergency_contacts script is called in an HTML document template by typing $ {template_script:employee_emergency_contacts} in the body of the Employee Profile HTML document template.

3. The Employee Profile HTML document template is selected on a case and the document template is generated with emergency contacts list as follows:

{#document-template-scripts__ol_a33_kl5_glb}

## Example of how document template script translates text in an HTML template {#document-template-scripts__section_kxn_35t_yyb}

Following is an employee_emergency_contacts script that populates the emergency contacts list in an Employee
Profile document.

`docTemplate` in this script references to the document template record, which helps in identifying the language and date format that are selected on the document template.
`getDisplayValueLang` is an API that helps in changing the language of dynamic tokens to the display language set in the Template language field in a document template.
`getByFormat` is an API that helps in displaying the date in the format set in the Template date format field in a document template.  

    (function runTemplateScript(target /*GlideRecord for target task*/, docTemplate /*GlideRecord for doc template*/) {

        //Add your code here to return the dynamic content for template
        var getHeaderCell = function(label) {
            return '<th style="border: 1px solid #dddddd; text-align: left; padding: 8px;">' + label + '</th>';
        };  
        var getDataCell = function(value) {
            return '<td style="border: 1px solid #dddddd; text-align: left; padding: 8px;">' + value + '</td>';
        };
        
        var html = '';
        var templateLang = docTemplate.getValue('language');
        var templateDateFormat = docTemplate.getValue('template_date_format');
        var hrTaskGr = new GlideRecord('sn_hr_core_contact');
        hrTaskGr.addQuery('user', target.getValue('subject_person'));
        hrTaskGr.query();
        while(hrTaskGr.next()) {
            var dob = hrTaskGr.getDisplayValue('date_of_birth');
            var grDOB = new GlideDateTime(dob);
            html = html + '<tr>';
            html = html + getDataCell(hrTaskGr.getDisplayValue('name'));
            html = html + getDataCell(hrTaskGr.getDisplayValue('mobile_phone'));
            html = html + getDataCell(hrTaskGr.getElement('relation_to_employee').getDisplayValueLang(templateLang));
            html = html + getDataCell(hrTaskGr.getElement('priority').getDisplayValueLang(templateLang));
            html = html + getDataCell(grDOB.getLocalDate().getByFormat(templateDateFormat)
            );
            html = html + '</tr>';
        }
        
        if(!gs.nil(html))
            html = '<h4>Emergency Contact Information</h4><table width="500px;"><tr>' + getHeaderCell('Name') + getHeaderCell('Mobile phone') + getHeaderCell('Relationship') + getHeaderCell('Priority') + getHeaderCell('Date of birth') + html + '</table>';
        
        return html;

    })(target, docTemplate);

Following is an example of how dynamic tokens are translated in an HTML doc template.

1. While configuring an HTML template, the template language is selected as German and date format is set to dd/MM/yyyy.
2. The HTML document template is referenced in an HR case.
3. When the agent previews the document, generates the attachment, or initiates document tasks for participants, priority and relationship fields are translated into the German language, and dates appear in the dd/MM/yyyy format.

{#document-template-scripts__ol_h5w_xmk_zyb}

*[\>]: and then


