Skip to content

Add Scheduled Job script to auto-refresh dashboard daily #1590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Background Scripts/Refresh Dashboards/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Automatic Dashboard Refresh Script for ServiceNow

This script, designed for ServiceNow, automatically refreshes a specified dashboard daily at 12:00 AM. By creating a Scheduled Job with this script, users can ensure their dashboard data is updated without manual intervention.

## Overview

ServiceNow’s Scheduled Jobs feature allows you to run server-side scripts at specified intervals. This script uses the `pa_dashboards` table to update the `last_refreshed` field on a given dashboard record, simulating a refresh.

## Prerequisites

- Access to ServiceNow with permissions to create and run Scheduled Jobs.
- The `sys_id` of the dashboard you want to refresh.

## Setup Instructions

1. **Navigate to Scheduled Jobs**:
- Go to **System Definition > Scheduled Jobs** in ServiceNow.

2. **Create a New Scheduled Job**:
- Set the job to run at **12:00 AM** daily.

3. **Add the Script**:
- Copy and paste the script below into the Scheduled Job’s script field.
- Replace `YOUR_DASHBOARD_SYS_ID` with the `sys_id` of the dashboard you want to refresh.

4. **Save and Activate the Job**:
- Ensure the job is active and will repeat daily.
19 changes: 19 additions & 0 deletions Background Scripts/Refresh Dashboards/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Define the sys_id of the dashboard you want to refresh
var dashboardSysId = 'YOUR_DASHBOARD_SYS_ID';

// Function to simulate the dashboard refresh
function refreshDashboard(dashboardSysId) {
// Use the Service Portal widget for dashboards to refresh them
var dashboardGR = new GlideRecord('pa_dashboards');
if (dashboardGR.get(dashboardSysId)) {
// Trigger dashboard refresh by updating a field or simulating a reload
dashboardGR.setValue('last_refreshed', gs.nowDateTime());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field does not exist on W and X instances. Additionally, ServiceNow dashboards load their data on open and can be refreshed through a page. Reports can be run on a schedule for emails

dashboardGR.update();
gs.info('Dashboard with sys_id ' + dashboardSysId + ' refreshed at ' + gs.nowDateTime());
} else {
gs.error('Dashboard with sys_id ' + dashboardSysId + ' not found.');
}
}

// Call the function to refresh the specified dashboard
refreshDashboard(dashboardSysId);
Loading