Salesforce Dashboard Rotation and Refresh with Tampermonkey

13 Jan

Have you ever wanted to rotate through a series of Salesforce dashboards and have them auto refresh? There are multiple questions on the success community and other forums requesting exactly this. The easiest way to rotate through dashboards is to use a browser plugin that switches between pages. However, data in the dashboards can only be scheduled to refresh once per day. A more frequent refresh requires a user to click the refresh button on the dashboard page.

Enter the Tampermonkey browser plugin and a little Javascript. Tampermonkey is a plugin that allows you to execute Javascript code when a page loads in the browser. To solve the problem of getting a refreshed rotation of dashboards, I wrote a little Javascript code that will redirect to a series of dashboard URLs and automatically click the refresh button without user interaction.

  1. Install the Tampermonkey plugin to Chrome or Firefox.
  2. Create a new Tampermonkey script and paste in the code below.
  3. Update the URLs from “https://na1.salesforce.com” to reflect your instance’s URL.
  4. Update the list of dashboard record ids in the code. Navigate to the dashboard page, and you will find the record id in the location bar of the browser.
  5. Navigate to a dashboard page, add the URL parameter “&rotate=true” to the end, and press enter.
// ==UserScript==
// @name         Salesforce Dashboard Rotator
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Rotate through  series of Salesforce dashboards. Refresh them after they have loaded.
// @author       David Helgerson
// @match        https://na1.salesforce.com/01Z*?rotate=true
// @grant        none
// @require      http://code.jquery.com/jquery-1.12.4.min.js
// ==/UserScript==

(function() {
    'use strict';

    /**
    * rotate through an array of dashboard page IDs
    */
    function changeDashboard() {
        var baseURL = 'https://na1.salesforce.com/';
        var dashboardIdList = [
            "01Z38000000M2z9",
            "01Z50000000j2ih",
            "01Z50000000j2ii"
        ];
        var arrayLength = dashboardIdList.length;
        var nextPage = '';
        var saveNext = false;
        for (var i = 0; i < arrayLength; i++) {
            if ('' === nextPage) nextPage = dashboardIdList[i];  // save first
            if (true === saveNext) {
                nextPage = dashboardIdList[i];
                break;
            }
            if (baseURL+dashboardIdList[i]+'?rotate=true' == window.location.href) {
                saveNext = true;
            }
        }
        if ('' !== nextPage) {
            window.location.href = baseURL+nextPage+'?rotate=true';
        }
    }
	
    /**
    * click the dashboard's refresh button
    */
    function autorefresh() {
        document.getElementById('refreshInput').click();
    }

    /**
    * remove elements at the top of the page, so dashboards have more screen real estate 
    */
    function removeHead() {
        $( "#AppBodyHeader" ).remove();
        $( ".ptBreadcrumb" ).remove();
    }

    // run after page loads
    window.addEventListener('load', function() {
        removeHead();
    }, false);

    // run delayed
    setTimeout(autorefresh, 27000);  // click the refresh after 27 seconds, so the dashboard will be refreshed when it appears on the next rotation.
    setTimeout(changeDashboard, 30000);  // rotate to the next dashboard after 30 seconds.
})();