Document template scripts
Summarize
Summary of Document Template Scripts
Document template scripts in ServiceNow enable dynamic modification of HTML document templates by embedding JavaScript code that can perform simple or complex tasks, such as displaying HR data or querying the database. These scripts improve template flexibility and reuse by allowing the same script to be referenced across multiple document templates using the${templatescript:scriptname}tag.
Show less
How to Use Document Template Scripts
Scripts are created under Document Templates > Document Templates Script. Once created, a script can be embedded in the HTML template body by referencing its name. The script runs with access to the target record (e.g., an HR case) and optionally the document template record, allowing it to tailor content dynamically.
Key Features
- Dynamic Content Generation: Scripts can query related data, format it in HTML tables, and return the result for inclusion in the document.
- Reusable Embedded Scripts: By embedding the script tag in multiple templates, the same logic can be reused without duplication.
- Localization Support: Scripts can access the document template’s language and date format settings to display translated field values and appropriately formatted dates, enhancing multinational usability.
- APIs for Localization: Methods like
getDisplayValueLang()andgetByFormat()enable scripts to render data in the selected language and preferred date format, ensuring compliance with locale-specific requirements.
Practical Example
The provided example script, employeeemergencycontacts, populates an emergency contacts table in an Employee Profile document. It queries emergency contact records linked to the employee, formats the data into an HTML table, and respects the language and date format settings of the document template. This allows HR cases referencing this template to generate personalized, localized documents that include up-to-date emergency contact information.
Benefits for ServiceNow Customers
- Enables creation of dynamic, data-driven documents tailored to specific case records.
- Simplifies maintenance by centralizing script logic for document content generation.
- Supports localization, improving document relevance for global organizations.
- Enhances end-user experience by delivering well-formatted, context-aware documents directly from ServiceNow cases.
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 to.
Example of how to create and use a document template script in an HTML template
- 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); - 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.
- The Employee Profile HTML document template is selected on a case and the document template is generated with emergency contacts list as follows:
Example of how document template script translates text in an HTML template
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);- While configuring an HTML template, the template language is selected as German and date format is set to dd/MM/yyyy.
- The HTML document template is referenced in an HR case.
- 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.