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
- Create a new user called pdf_export_account (or similar, or completely different, up to you).
- 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.
- 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.
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.
Things to note or look for if you have problems when using this script.
- If you use this in a business rule, make sure you set it to Async
- 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.
- Make sure your properties are all set up correctly
- Make sure your system URI property is returning the right value, an example would be (https://<<my_instance_name>>.service-now.com/)
- Make sure your new users username and password are correct
- Make sure you aren't getting blocked by other means (firewall/proxy etc)