How to Automatically Convert a Dashboard to PDF in ServiceNow - Need Help!

Andrea34
Tera Contributor

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:

  1. Name: Send Dashboard PDF
  2. Table: Change Request
  3. When to send: [Choose the appropriate condition, e.g., "Updated"]
  4. Who will receive: [Select the appropriate recipients]
  5. 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:

  1. Event: dashboard.pdf.generated
  2. Condition: Add a condition if necessary
  3. 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!

3 REPLIES 3

Community Alums
Not applicable

Hi @Andrea34 

 

Have you considerd the OOTB functionality. Please refer below.

https://www.servicenow.com/docs/bundle/xanadu-now-intelligence/page/use/performance-analytics/task/t...

 

Please like/mark my response as useful if it is inline with your expectation.

Regards,

Gagan k

Hi!

 

Thanks for your answer. In this case, I need to do everything automatically through code, if possible, not manually.

 

 

Community Alums
Not applicable

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