Unable to Display Images in Word Document Generated via GlideSysAttachment

prithviaram
Tera Contributor

Requirement:
I am looking for a solution that allows displaying an image inside a Word document generated using the background script shown below.

Problem Statement:
I need to embed and display an image inside a Word document that is created through a server-side script.

Code:

 
// ===== Get Target Record =====
var userGR = new GlideRecord('sys_user');
userGR.get(gs.getUserID());

// ===== File Name =====
var now = new GlideDateTime();
var safeStamp = ('' + now.getDisplayValue()).replace(/[:\s]/g, '_');
var fileName = 'ServiceNow_Explanation_' + safeStamp + '.doc';

// ===== HTML Content =====
var html =
'<html><body style="font-family:Calibri;font-size:11pt;">' +
'<h1>What is ServiceNow?</h1>' +
'<p>ServiceNow is a cloud-based platform used to manage and automate digital workflows.</p>' +
'<p>It is widely used for IT Service Management (ITSM) to manage incidents, service requests, problems, and changes.</p>' +
'<p>The platform enables automation through workflows, Flow Designer, integrations, and low‑code application development.</p>' +
'<p>ServiceNow also supports HR Service Delivery, Customer Service Management, Security Operations, and IT Operations.</p>' +
'<p>Overall, ServiceNow helps organizations improve efficiency and deliver better digital services.</p>' +
'</body></html>';

// ===== Create Attachment =====
var gsa = new GlideSysAttachment();
var attachmentSysId = gsa.write(userGR, fileName, 'application/msword', html);

if (attachmentSysId) {
    gs.info('Word document created successfully');
} else {
    gs.error('Attachment creation failed');
}

Approaches Tried:
I attempted to display an image by:

  1. Referencing the image directly from the sys_db_image table, and
  2. Converting the image into Base64 and embedding it in the HTML.

Both approaches displayed the image correctly in online HTML previewers, but Word did not render the image. In some cases, Word displayed placeholder text or broken image icons.

Conclusion:
I would like to know whether it is possible to embed and display an image inside a Word document generated through this method in ServiceNow, and if so, what approach works reliably.

1 REPLY 1

Sanjay191
Kilo Patron

Hi @prithviaram 

you can use the document template as well for this , to generate the document.
but if you want to use the custom script then you need to create image url and you need to use it into the html with proper tags for the image 

 imageUrl = '/sys_attachment.do?sys_id=' + sys_id of the image (make sure this should be present on the sys_attachment) and if you use the image table then you can change it with your image table

<img src=imageUrl  alt="image">

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You