logo

NJP

Embed Reports as Images in a Scheduled Report Emails

Import · Oct 01, 2019 · article

To save time, users want to open an email on their phone and quickly see if they need to look more at an issue or if they can move on.

By default, ServiceNow attaches reports (whether sent as a PDF or PNG) to emails when emailing a scheduled report which requires the recipient to open the email, then open the attachment. This does not always meet our users’ expectations.

With the power of the NOW Platform, we can apply a small customization to embed graphical reports as images in your emails to deliver emails like this to your users:

image

Create Business Rule to embed images

Create a new business rule on sys_email to modify the content of emails to embed the images before the record is first inserted.

Fill out the form with the following values:

Name: Inline Report Images

Table: Email [sys_email]

When to run

When: before

Insert: checked

Filter Conditions

Target table [is] sysauto_report

Advanced

Script:

(function executeRule(current, previous /*null when async*/) { 
  // check if tag is found in body 
  if(current.body.indexOf('[embedded report]') == -1) 
  { 
    // if not found, leave everything alone 
    return; 
  } 
   // get email attachments and set them to inline attachments 
  var attach = new GlideRecord('sys_email_attachment'); 
  attach.addQuery('email', '=', current.sys_id); 
  attach.addQuery('attachment.content_type', 'STARTSWITH','image/'); // only images supported 
  // set any attachments as being inline so they aren't attached 
  attach.setValue('content_disposition', 'inline'); 
  attach.updateMultiple(); 
  // get the attachments to loop over 
  attach.query(); 
  // if  
  if(attach.getRowCount() == 0) 
  { 
    gs.warn('No attachments, so nothing to inline'); 
    current.body = current.body.replace('[embedded report]', 'Nothing to attach'); 
    return; 
  } 
  var reportContent = ''; 
  // add tags to inline all the images 
  while(attach.next()) 
  { 
    reportContent += '<img id="img_' + attach.attachment + '" alt="' + attach.attachment.getDisplayValue() + '" src="sys_attachment.do?sys_id=' + attach.attachment + '"/>'; 
  } 
  // replace the tag in quotes since that can't have tags added to it.  This should be a smarter regex, but we'll brute force it for now 
  current.body = current.body.replace('\'[embedded report]\'', ''); 
  current.body = current.body.replace('"[embedded report]"', ''); 
  // add the image tags 
  current.body = current.body.replace('[embedded report]', reportContent); 
  return; 
})(current, previous); 

On a scheduled report, specify where to put the images

Placing the string “[embedded report]” in the Introductory message. It is recommended to make this text a link back to the desired report or dashboard to drive engagement with the live data in the source instance.

image

Only image files (PNG) reports are in-lined in the email text. Other files (PDF or Excel) will be attached to the email.

If multiple reports are sent at the same time (with the “Include with” option), all the images will be included in the email, however, the order of the images is not guaranteed.

Additional Documentation

Schedule a report - https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/reference-pages/t...

Automate report distribution - https://docs.servicenow.com/bundle/newyork-performance-analytics-and-reporting/page/use/reporting/ta...

Create a business rule - https://docs.servicenow.com/bundle/newyork-application-development/page/script/business-rules/task/t...

View original source

https://www.servicenow.com/community/platform-analytics-articles/embed-reports-as-images-in-a-scheduled-report-emails/ta-p/2302586