Save a Record as a PDF Attachment to itself in ServiceNow

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.

To achieve this functionality we require the use of the RESTMessageV2 engine. In essence we are calling a web service on our own instance from within our own instance, it might seem a little backwards but I assure you it works.

The following steps will help you achieve exporting a record as a PDF and attaching it back to itself.

Create a new User that will perform the export tasks

  1. Create a new user called pdf_export_account (or similar, or completely different, up to you).
  2. Create a system property called pdf.export.username (again the name is up to you, just remember it later for your script). In the value, write the name of your newly created user from step 1.
Sys Property example for the username, must match script
  1. Create a system property called pdf.export.password (again the name is up to you, just remember it later for your script). In the value, write the password you set for your newly created user in step 1.
Sys Property example for the password, must match script

Create the script that performs the export action

Now, you can do this from any number of places, you can do it from Scripts - Background, or you could put it in a business rule, or you could put it in a Script Include, its really up to you where you want to implement this from.

Note: If putting this in a business rule, please note that the business rule must be set to run on Async , otherwise RESTMessageV2 will not run.

// Initialise the RESTMessageV2 Engine

var rm = new sn_ws.RESTMessageV2();

// Set the HTTP Method to GET because we are performing a URL navigation essentially

rm.setHttpMethod('GET');

// Grab the Instances URI from the out of the box system property and generate a URL
// Replace current.getTableName() with a hard coded table name if you need to or with another method of grabbing the table name you wish to use
// Same again with current.sys_id

var url = gs.getProperty("glide.servlet.uri") + current.getTableName() + '.do?PDF&sys_id=' + current.sys_id;

// Set the endpoint URL

rm.setEndpoint(url);

// Set Basic Auth for the request, using our stored Username and Password

rm.setBasicAuth(gs.getProperty('pdf.export.username'), gs.getProperty('pdf.export.password'));

// Once the response is asked for, we save this response (containing a PDF) against the record
// Replace current.getTableName() and current.sys_id again as needed, this could even be on a different record if you like (cool!)

rm.saveResponseBodyAsAttachment(current.getTableName(), current.sys_id, current.number + ".pdf");

// Execute the call

var response = rm.execute();

Server Script

Things to note or look for if you have problems when using this script.

  1. If you use this in a business rule, make sure you set it to Async
  2. You CAN use this in a flow action, which is nice, and you can then trigger said flow actions from a NON Async business rule, if you require setting your business rule to run On After etc so that you can use conditions.
  3. Make sure your properties are all set up correctly
  4. Make sure your system URI property is returning the right value, an example would be (https://<<my_instance_name>>.service-now.com/)
  5. Make sure your new users username and password are correct
  6. Make sure you aren't getting blocked by other means (firewall/proxy etc)