From 0cd49a6701c3b850224f6e71173ffe222378bf41 Mon Sep 17 00:00:00 2001 From: Ayush <86900579+Southpaw09@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:41:32 +0530 Subject: [PATCH] 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. --- .../Refresh Dashboards/README.md | 27 +++++++++++++++++++ .../Refresh Dashboards/script.js | 19 +++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Background Scripts/Refresh Dashboards/README.md create mode 100644 Background Scripts/Refresh Dashboards/script.js diff --git a/Background Scripts/Refresh Dashboards/README.md b/Background Scripts/Refresh Dashboards/README.md new file mode 100644 index 0000000000..b6df78dcfd --- /dev/null +++ b/Background Scripts/Refresh Dashboards/README.md @@ -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. diff --git a/Background Scripts/Refresh Dashboards/script.js b/Background Scripts/Refresh Dashboards/script.js new file mode 100644 index 0000000000..a9104e275b --- /dev/null +++ b/Background Scripts/Refresh Dashboards/script.js @@ -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()); + 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);