How to Automatically Convert a Dashboard to PDF in ServiceNow - Need Help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2025 08:03 AM
Hello everyone, I hope you're all doing well. I need your help because I'm trying to achieve the following: I want to see if it's possible to automatically convert a dashboard into a PDF, similar to the "Export to PDF" option but done automatically. Here are the steps I've taken and the code I have so far:
1. Create the Dashboard and Reports
Ensure that the dashboard you want to send is properly set up and contains all the necessary reports.
2. Create a Script Include to Generate the Dashboard PDF
You'll need a Script Include that can generate a PDF of the dashboard. Here's a basic example:
var DashboardPDFGenerator = Class.create();
DashboardPDFGenerator.prototype = {
initialize: function() {},
generatePDF: function(dashboardSysId) {
// Logic to generate the PDF of the dashboard
// This might involve using a third-party library or API
var dashId = dashboardSysId.toString();
gs.print("Line 10: " + dashId);
// var pdfData = this._generatePDFData(dashId);
return dashId;
},
_generatePDFData: function(dashboardSysId) {
// Implementation to fetch and convert the dashboard to PDF
// This part may vary depending on your setup
return "PDF data for dashboard with sys_id: " + dashboardSysId;
},
type: 'DashboardPDFGenerator'
};
3. Create an Email Notification
Navigate to System Notification > Email > Notifications and create a new notification:
Email Notification Details:
- Name: Send Dashboard PDF
- Table: Change Request
- When to send: [Choose the appropriate condition, e.g., "Updated"]
- Who will receive: [Select the appropriate recipients]
- What it will contain:
- Subject: Your Dashboard PDF
- Message: Here is the PDF with the latest reports.
4. Attach the PDF to the Notification
To attach the PDF, use a ServiceNow event to generate and attach the PDF. Here's an example of how you could do it:
(function() {
var dashboardSysId = "YOUR_DASHBOARD_SYS_ID"; // Replace with your dashboard's sys_id
var pdfGenerator = new DashboardPDFGenerator();
var pdfData = pdfGenerator.generatePDF(dashboardSysId);
var attachment = new GlideSysAttachment();
var attachmentSysId = attachment.write(current, 'PDF', 'dashboard.pdf', pdfData);
gs.eventQueue('dashboard.pdf.generated', current, attachmentSysId, current.sys_id);
})();
5. Configure the Event and Notification
Link the event to the notification:
- Event: dashboard.pdf.generated
- Condition: Add a condition if necessary
- Email Script for attachment
var attachmentSysId = event.parm1;
email.addAttachment(attachmentSysId);
However, as you can see, the Script Include lacks the logic to actually convert the dashboard into a PDF. Right now, it just creates a file with the dashboard ID inside. Could you please help me add the necessary logic to convert the dashboard to a PDF?
Thanks guys!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-12-2025 09:13 AM
Hi @Andrea34
Have you considerd the OOTB functionality. Please refer below.
Please like/mark my response as useful if it is inline with your expectation.
Regards,
Gagan k
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 05:41 AM
Hi!
Thanks for your answer. In this case, I need to do everything automatically through code, if possible, not manually.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-13-2025 05:46 AM
Hi @Andrea34 ,
I didn't get your question right. The OOTB functionality can do what you want to without writing code. So, I would suggest you to first check the link.
Please like/mark my response as useful if it is inline with your expectation.
Regards,
Gagan k