- 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
06-18-2025 06:31 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 10:33 AM
Hello,
1. same script working for me with same code from knowledge article body, try for that and check for any changes for any other source.
2. Please check if you are using .doc not .docx is script include becasue in your error, i still see .docx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 10:46 AM
Hi @Muhammad Salar ,
Thank, yes its working now.
Instead of writing html code directly in here, we have a sn_doc_html_template table, there i created a record with my html content, can i call that record and convert to doc, can you please help me on this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 11:57 AM - edited 06-18-2025 12:07 PM
Hello,
Try changing the table in query
and field values here
var content = HtmlHead + '<h1>' + gr.getDisplayValue('short_description') + '</h1>' + '<p style="margin-top:-20px;">' + gr.getDisplayValue('number') + '<b>' + ' . ' + '</b>' + gr.getDisplayValue('version.version') + '<b>' + ' . ' + '</b>' + gr.published + '</p>' + '</p>' + '<br>' + '<div style="margin-top:-30px;">' + gr.getValue("text") + '</div>';
like gr.getValue("text") is the main body here, it should work
OR
how are you coverting into pdf? any code
What is field type?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 12:00 PM
this is the UI Action code
this is script include,