The CreatorCon Call for Content is officially open! Get started here.

PDF Generation

sunilsargam
Tera Guru

Hello Team,

 

Can we generate the PDF of the record eg. incident when the ticket is closed? It should automatically get attached to the respective incident

 

Thank you

Sunil

2 REPLIES 2

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @sunilsargam 

https://www.servicenow.com/community/itsm-forum/i-want-to-create-a-one-pdf-file-for-incident-record-...

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

raviteja1600
Tera Guru

Hi @sunilsargam ,

 

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
  3. 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.

// 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();

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)

If my response is helpful to you, please mark it helpful and accept the solution.

 

Regards,

Raviteja