Save Record as PDF Attachment in ServiceNow

Here we provide a method we have found for exporting a ServiceNow record as a PDF file and attaching it back to the record.

In ServiceNow you often have the need to export a record as a PDF, whilst this is not necessarily difficult, occasionally you want to take this a step further and have this PDF automatically attached back onto the originating record as an attachment.

Below we outline a basic set of scripts that help you achieve this.

We start with a Server Script that uses a new custom user account to read and export the record, this record is then saved back to the originating record as an attachment.

var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
var url = gs.getProperty("glide.servlet.uri") + current.getTableName() + '.do?PDF&sys_id=' + current.sys_id;
rm.setEndpoint(url);
rm.setBasicAuth(gs.getProperty('pdf.export.username'), gs.getProperty('pdf.export.password'));
rm.saveResponseBodyAsAttachment(current.getTableName(), current.sys_id, current.number + ".pdf");
var response = rm.execute();
Server Script

This script requires setting up 2 system properties, a username and a password that correspond to a user account that is used to generate the PDF.

Make sure that you have a user account that matches these system properties other wise this will not work.

Sys Property example for the username, must match script
Sys Property example for the password, must match script