Oh my god. It's full of code!

Salesforce Dashboard Automatic Refresh Bookmarklet

Hey all,

Quick fun little chunk of code here for you. This code when saved as a bookmarklet (javascript saved as a bookmark which runs on the current page when clicked) will cause Salesforce dashboards to automatically refresh every X seconds, where X is a variable near the top of the code (defaults to 90 seconds). It also injects a little timer on the refresh button, and is smart enough to wait for the dashboards to refresh before it continues the next countdown. I haven’t cross browser tested it yet (built in Chrome 25) but as long as the browser supports the DOMSubtreeModified event listener you are probably fine. Just save the code as a bookmarklet, navigate to your dashboard page and click the bookmarklet. You should see a small timer show up on the refresh button. When the timer hits 0 the dashboard should refresh, and the timer will reset back to the default time and being counting down again.

javascript:(
    function() 
    {
        var refreshInterval = 90; //number of seconds between each refresh
        var counter = refreshInterval;
        var timerInterval;
        var button = document.getElementById('refreshInput');
        if(button == null)
        {
            alert('Refresh Button not found! Salesforce may have changed the buttons ID or it may not be visiable for some reason. Please make sure you are on a dashboard page with the Refresh button visible');
            return false;
        }

        document.addEventListener("DOMSubtreeModified", function() {
            if(event.target.id == "componentContentArea")
            {
                startTimer();
            }
        }, true);

        function countDown(){
            counter--;
            button.value = "Refresh ("+formatTime(counter)+")";
            if(counter == 0)
            {
                button.click();
                counter = refreshInterval;
                window.clearInterval(timerInterval);            
                button.value = "Waiting for Refresh";
            }                
        }

        function startTimer()
        {
            window.clearInterval(timerInterval);
            timerInterval = setInterval(countDown, 1000);     
        }    

        function formatTime(seconds)
        {
            var totalSec = seconds;
            hours = parseInt( totalSec / 3600 ) % 24;
            minutes = parseInt( totalSec / 60 ) % 60;
            seconds = totalSec % 60;

            result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);            

            return result;
        }
        startTimer(); 
    }
)();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 594 other followers