PDF Generation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @sunilsargam
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]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
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
- 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.
// 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.
- 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)
If my response is helpful to you, please mark it helpful and accept the solution.
Regards,
Raviteja
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Yes, this can be achieved by utilizing the PDFGenerationAPI provided by the platform....To implement this, you can create a Business Rule that triggers when the Incident record state changes to Closed. Within this Business Rule, you can use the PDFGenerationAPI to generate a PDF of the record's form view. The generated PDF can then be attached to the Incident record using the Attachment API....
var pdf = new sn_pdfgeneratorutils.PDFGenerationAPI();
var htmlContent = "<h1>Incident Details</h1><p>" + current.short_description + "</p>";
var pdfDocument = pdf.convertToPDF(htmlContent, 'incident', current.sys_id, 'Incident Report');
var attachment = new GlideSysAttachment();
attachment.write(current, 'incident_report.pdf', 'application/pdf', pdfDocument);
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @sunilsargam ,
You can use the After Business rule with the condition "State changes to Resolve" to create the PDF and attach it to the Incident.
Use the below code and let me know if any issues you face.
(function executeRule(current, previous) {
var pdfAPI = new sn_pdfgeneratorutils.PDFGenerationAPI();
// Build HTML content for the PDF
var html = "<h1>Incident Summary</h1>" +
"<p><strong>Number:</strong> " + current.number + "</p>" +
"<p><strong>Short Description:</strong> " + current.short_description + "</p>" +
"<p><strong>Caller:</strong> " + current.caller_id.getDisplayValue() + "</p>" +
"<p><strong>Assignment Group:</strong> " + current.assignment_group.getDisplayValue() + "</p>" +
"<p><strong>State:</strong> " + current.state.getDisplayValue() + "</p>" +
"<p><strong>Resolution Notes:</strong> " + current.close_notes + "</p>";
// Add Work Notes
var journal = new GlideRecord('sys_journal_field');
journal.addQuery('element_id', current.sys_id);
journal.addQuery('element', 'work_notes');
journal.query();
html += "<h2>Work Notes</h2><ul>";
while (journal.next()) {
html += "<li><strong>" + journal.sys_created_by + "</strong>: " + journal.value +
" <i>" + journal.sys_created_on + "</i></li>";
}
html += "</ul>";
// Add Additional Comments
var journalCom = new GlideRecord('sys_journal_field');
journalCom.addQuery('element_id', current.sys_id);
journalCom.addQuery('element', 'comments');
journalCom.query();
html += "<h2>Additional Comments</h2><ul>";
while (journalCom.next()) {
html += "<li><strong>" + journalCom.sys_created_by + "</strong>: " + journalCom.value +
" <i>" + journalCom.sys_created_on + "</i></li>";
}
html += "</ul>";
// Optional: Replace with your actual font sys_id or leave blank
var fontSysId = ''; // Example: 'a1b2c3d4e5f6g7h8i9j0'
// Generate and attach PDF
var result = pdfAPI.convertToPDF(html, 'incident', current.sys_id, current.number + "_Summary.pdf", fontSysId);
gs.info("PDF Generation Result: " + JSON.stringify(result));
})(current, previous);
If the provided information is helpful to you, please mark it as helpful and accept the solution.
Regards,
Raviteja
