Skip to content

Commit 0cd49a6

Browse files
authored
Add Scheduled Job script to auto-refresh dashboard daily
- Created a script to automatically refresh a specified dashboard at 12 AM every day. - Implemented functionality to update the `last_refreshed` field on the target dashboard in `pa_dashboards` to simulate refresh. - Added README.md with setup instructions, script details, and usage notes. This feature ensures dashboards are refreshed without manual intervention, enhancing data relevance for daily usage.
1 parent 0e05c51 commit 0cd49a6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Automatic Dashboard Refresh Script for ServiceNow
2+
3+
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.
4+
5+
## Overview
6+
7+
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.
8+
9+
## Prerequisites
10+
11+
- Access to ServiceNow with permissions to create and run Scheduled Jobs.
12+
- The `sys_id` of the dashboard you want to refresh.
13+
14+
## Setup Instructions
15+
16+
1. **Navigate to Scheduled Jobs**:
17+
- Go to **System Definition > Scheduled Jobs** in ServiceNow.
18+
19+
2. **Create a New Scheduled Job**:
20+
- Set the job to run at **12:00 AM** daily.
21+
22+
3. **Add the Script**:
23+
- Copy and paste the script below into the Scheduled Job’s script field.
24+
- Replace `YOUR_DASHBOARD_SYS_ID` with the `sys_id` of the dashboard you want to refresh.
25+
26+
4. **Save and Activate the Job**:
27+
- Ensure the job is active and will repeat daily.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Define the sys_id of the dashboard you want to refresh
2+
var dashboardSysId = 'YOUR_DASHBOARD_SYS_ID';
3+
4+
// Function to simulate the dashboard refresh
5+
function refreshDashboard(dashboardSysId) {
6+
// Use the Service Portal widget for dashboards to refresh them
7+
var dashboardGR = new GlideRecord('pa_dashboards');
8+
if (dashboardGR.get(dashboardSysId)) {
9+
// Trigger dashboard refresh by updating a field or simulating a reload
10+
dashboardGR.setValue('last_refreshed', gs.nowDateTime());
11+
dashboardGR.update();
12+
gs.info('Dashboard with sys_id ' + dashboardSysId + ' refreshed at ' + gs.nowDateTime());
13+
} else {
14+
gs.error('Dashboard with sys_id ' + dashboardSysId + ' not found.');
15+
}
16+
}
17+
18+
// Call the function to refresh the specified dashboard
19+
refreshDashboard(dashboardSysId);

0 commit comments

Comments
 (0)