- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 12:38 PM
Hi,
We are currently working on a proof of concept (POC) in our Personal Developer Instance (PDI) within ServiceNow. As part of this POC, we are generating a PDF file from an HTML Template and attaching it to the Risk Response Task table.
However, instead of a PDF, our requirement is to generate and attach a Word (DOCX) document. We would like to know if there is a native or recommended way in ServiceNow to convert a PDF file to a Word (DOCX) file.
To explore alternatives, we attempted two different approaches:
Custom Azure-hosted Solution: We developed our own solution to convert PDFs to DOCX, hosted on Azure. While the integration with ServiceNow was successful, the resulting file was corrupted. Upon checking Azure logs, it appears that the file being received is already corrupted at the time of transmission.
Open Source API (ConvertAPI): We also integrated ConvertAPI to achieve the conversion. Unfortunately, the result was the same — a corrupted Word document was returned.
Given these challenges, we are seeking guidance or a working solution to successfully convert and attach a Word (DOCX) file in the Risk Response Task record in ServiceNow.
We would greatly appreciate any support or recommendations you could provide.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2025 09:14 AM - edited 06-20-2025 09:15 AM
Hello @srujancherr , These are simple things you can do this on your own by modifying it,
For images, i am not 100% sure.
If your original query is answered then Mark helpul and Accept it as solution
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 07:30 AM
Hi @srujancherr
You can check all attached variables in script and then set their values like this.
Just attach any incident values in template and use this script, it will dynamically change those variables into values.
getContentForWordTemp: function(temp, rec) {
var tempGr = new GlideRecord('sn_doc_html_template');
tempGr.addQuery('sys_id', temp);
tempGr.query();
while (tempGr.next()) {
var text = tempGr.getValue('html_script_body');
var recGr = new GlideRecord('incident');
if (!recGr.get(rec)) {
return;
}
var matches = text.match(/\$\{([^}]+)\}/g);
if (matches) {
for (var i = 0; i < matches.length; i++) {
var placeholder = matches[i];
var field = placeholder.replace('${', '').replace('}', '');
var value = recGr.getDisplayValue(field) || '';
var regex = new RegExp('\\$\\{' + field + '\\}', 'g');
text = text.replace(regex, value);
}
}
var HtmlHead = "<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word' " +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><meta charset='utf-8'></head><body>";
var content = HtmlHead;
content += "<h1><strong>" + tempGr.getDisplayValue('name') + "</strong></h1><br>";
content += "<div>" + text + "</div>";
content += "</body></html>";
var fileName = tempGr.getDisplayValue('name') + '.doc';
var contentType = 'application/msword';
var attachment = new GlideSysAttachment();
var attachmentID = attachment.write(recGr, fileName, contentType, content);
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 05:31 AM
u_introduction_clause, which table field is this?
do you want to show this field value in generated document?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 05:37 AM
It is a custom field from Incident table, similarly I have many custom fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 05:43 AM
What i remember is that you were creating document from sn_doc_html_template table.
are you still creating document from same table and want to show some incident fields as well in document??
Is this a correct assumption?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 06:11 AM
Yes @Muhammad Salar , exactly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2025 06:33 AM
Try this:
Heading, template name
next line, some incident fields for your reference
Then template body
var tempGr = new GlideRecord('sn_doc_html_template');
tempGr.addQuery('sys_id', temp);
tempGr.query();
while (tempGr.next()) {
var text = tempGr.getValue('html_script_body');
var fileName = tempGr.getDisplayValue('name') + '.doc';
var contentType = 'application/msword';
var HtmlHead = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'></head><body>";
var recGr = new GlideRecord('incident');
recGr.get(rec);
var content = HtmlHead;
content += "<h1><strong>" + tempGr.getDisplayValue('name') + "</strong></h1>";
content += "<p>";
content += "Number: " + recGr.getDisplayValue('number') + " | " +
"Caller: " + recGr.getDisplayValue('caller_id') + " | " +
"Short Description: " + recGr.getDisplayValue('short_description') + "<br>";
content += "</p>";
content += "<br>";
content += "<div>" + text + "</div>";
content += "</body></html>";
var attachment = new GlideSysAttachment();
var attachmentID = attachment.write(recGr, fileName, contentType, content);