Generating and Emailing PDF Reports from a Customized Homepage - ben-vargas/servicenow-wiki GitHub Wiki

When you need to distribute reports from a customized ServiceNow homepage as a PDF, you can combine the Webkit HTML to PDF Plugin, a dedicated system account, and a scheduled job. By following the steps outlined below, you can automatically generate and email a PDF version of a homepage, making it easy for users to review critical information without logging into the platform.


Prerequisites

  1. Webkit HTML to PDF Plugin Installed:
    Ensure that the Webkit HTML to PDF plugin is activated in your instance. This plugin provides the functionality to render webpages as PDFs.

  2. Dedicated System Account:
    Create a service account that will be used solely for generating and emailing these PDF exports. This user’s sys_id will be referenced in the scheduled job.


Steps

1. Install/Activate the Webkit HTML to PDF Plugin

  • Navigate to System Definition > Plugins.
  • Search for Webkit HTML to PDF.
  • Click Activate/Upgrade if it’s not already active.
  • Once activated, the plugin provides APIs and features for converting HTML content into PDF format.

2. Create a System Account for the PDF Export

  • Go to User Administration > Users.
  • Click New and create a user (e.g., pdf_export_user).
  • Assign necessary roles if required. This user will serve as the "Created By" user for the export emails, allowing you to identify the PDF export messages and customize them.

Why create a dedicated account?
By using a unique system user, you can easily differentiate PDF export emails from other outbound emails. This enables you to apply specific business rules for customizing email subjects and bodies, ensuring the emailed PDF is more user-friendly and report-like.

3. Create a Business Rule on sys_email for Customizing the Email

This Business Rule modifies the subject and body of the email generated by the PDF export job. It uses the "Created By" field to identify when the system account sends a PDF, then updates the email’s content for clarity.

Example Script:

(function executeRule(current, previous /*null when async*/) {

  var instanceName = gs.getProperty('instance_name');
  var homepageURL = 'https://' + instanceName + '.service-now.com/';
  homepageURL += 'nav_to.do?uri=home.do';
  homepageURL += encodeURIComponent('?sysparm_view=crmadoption');

  // Modify the email subject and body to be more descriptive
  current.subject = 'Techport Sales CRM - Daily Adoption';
  current.body = 'Attached is the Sales CRM Daily Adoption report for your review.'
    + '<br><br>'
    + '<a title="Sales CRM: Daily Adoption" href="' + homepageURL + '">Sales CRM: Daily Adoption</a>'
    + '<br><br>'
    + current.body;

})(current, previous);

Key Points:

  • This rule is triggered on sys_email so that when the PDF export email is created, it checks if it's from the system account.
  • It then updates the subject and body to be more meaningful and user-friendly, including a direct link back to the homepage view.

4. Create a Scheduled Job to Invoke Webkit for the Desired Homepage

The scheduled job triggers the PDF generation and sends it to the specified recipients.

How to Create the Scheduled Job:

  • Navigate to System Scheduler > Scheduled Jobs > Scheduled Scripts.
  • Create a new job (e.g., "TP13 Sales CRM - Adoption Homepage").
  • Action: Set a start date/time for when the job should first run.
  • In the Job context field, define parameters needed for the PDF generation:

Example Job Context:

# Example Job Context
fcUserID=1d5ae1e32b0452005f97284269da1506
fcDebug=false
[email protected],[email protected]
fcOptions={"dataDoctype":true,"orientation":"Landscape","paperSize":"Letter","smartShrink":true,"avoidPageBreakInside":true,"zoomFactor":"75"}
fcURL=/home.do?sysparm_view=crmadoption

Notes:

  • fcUserID: Set this to the sys_id of the system account created in Step 2.
  • fcEmail: Comma-separated list of recipients.
  • fcURL: The relative URL to the homepage with the desired view. Ensure the sysparm_view matches the homepage’s view.
  • fcOptions: JSON containing PDF rendering options (orientation, paper size, etc.).

Trigger Class: Set Trigger class to com.glide.job.WHTPExportJob to leverage the Webkit HTML to PDF functionality.

Once configured, the scheduled job will run at the specified interval, generate the PDF, and send it via email. The Business Rule from Step 3 will transform the email’s subject and body into a user-friendly format.


Best Practices

  • Testing in Non-Production:
    Always test PDF generation and email styling in a development environment first. Validate the email content, PDF formatting, and ensure the correct recipients.

  • Keep Views Simple: For best PDF results, use a homepage view designed for printing or exporting. Complex layouts might not render perfectly in PDF.

  • Debugging: If something goes wrong, check system logs for errors from the scheduled job or the business rule. You can temporarily enable fcDebug=true for more output.


Conclusion

By leveraging the Webkit HTML to PDF plugin, a dedicated system account, a custom business rule, and a scheduled job, you can streamline the process of distributing homepage-based reports as PDF attachments. This approach ensures recipients receive well-formatted, easy-to-understand reports directly via email—no manual exports required.

⚠️ **GitHub.com Fallback** ⚠️