Can anyone help me with generating PDF of a record using custom script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wednesday
We have a requirement to send PDF of a record as attachment in notification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
Hello @harikar51538639 ,
That is a classic requirement, and while ServiceNow doesn't have a single "checkbox" to do this, a brilliant developer handles this by leveraging the PDFGenerationAPI combined with SysAttachment.
Since we want this to be automated and clean, I’ll give you the most efficient way to do this using a Script Action or a Business Rule that triggers an event.
The Strategy
- Generate the PDF: Use the sn_pdfgeneratorutils.PDFGenerationAPI.
- Attach to Record: Save that PDF to the target record (sys_attachment).
- Trigger Notification: Fire an event that includes the attachment.
Step 1: The Bravely Written Script
You can place this in a Business Rule (after insert/update) or a Script Include.
// Define the table and recordvar table = current.getTableName();var sysId = current.getUniqueValue();var fileName = "Record_" + current.number + ".pdf";
// 1. Setup the PDF APIvar pdfApi = new sn_pdfgeneratorutils.PDFGenerationAPI();
// 2. Define the HTML content (You can pull this from a template or build it here)var htmlContent = "<html><body><h1>Record Summary: " + current.number + "</h1>";
htmlContent += "<p>Short Description: " + current.short_description + "</p>";
htmlContent += "<p>Created by: " + current.sys_created_by + "</p></body></html>";
// 3. Generate and Attach// Parameters: HTML, Target Table, Target SysID, Filenamevar result = pdfApi.convertToPDF(htmlContent, table, sysId, fileName);
// 4. Fire Event to send Notification// Ensure you have an event created in 'sysevent_register'gs.eventQueue("x_your_app.send_pdf_notification", current, current.assigned_to, "");Step 2: Configure the Notification
Once the script attaches the file to the record, the notification needs to know it should "grab" it.
- Navigate to System Notification > Email > Notifications.
- Open your specific notification.
- Under the What it will contain tab, check the box: Include attachments.
Important Developer Notes (The "Awesome" Way)
- Timing is Everything: If you generate the attachment and fire the event in the same script, ServiceNow is usually fast enough to include it. However, if the PDF is huge, you might want to add a small delay or use a gs.sleep() (though I'd prefer a cleaner asynchronous approach).
- PDF Templates: Instead of hardcoding HTML in the script, I recommend using Document Templates. You can fetch a pre-designed template and pass the current object into it to fill the fields dynamically.
- The "Attachment Link" Trick: If you don't want to bloat the user's inbox, you can also use the ${attachment_link} mail script in the notification body instead of checking "Include attachments."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday - last edited Thursday
Option 1: Create a After/insert BR with following code:
Refer:Attach record PDF file to email notification
https://www.servicenow.com/community/itsm-forum/attach-pdf-to-email-notification/m-p/2567121
Script:
(function executeRule(current, previous /*null when async*/) {
var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
var url = 'https://<instance_name>.service-now.com/' + current.target_table + '.do?PDF&sys_id=' + current.instance;
rm.setEndpoint(url);
rm.setBasicAuth(gs.getProperty('sys.properties.userid'), gs.getProperty('sys.properties.password'));
rm.saveResponseBodyAsAttachment(current.getTableName(), current.sys_id, current.target_table +".pdf");
var response = rm.execute();
})(current, previous);
Option 2: (For HR only) There is free stored app sn_doc . using some script you can generate the pdf .
Refer: UI Action to Generate PDF and Attach to Record
- Enable multiple participants to collaboratively work on a single document
- Collect repetitive information and furnish it wherever necessary
- Notify the participant
- Secure document
- Choose from Multiple Signing Types
- Table of Content configuration
- Page Number configuration
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thursday
when is the email sent and how?
share some details
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
