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.
Continue reading

Alert with Option Buttons in the Window Body – jQueryUI in a Salesforce Custom Button

5 Nov

Here is another example of using jQueryUI in a Salesforce custom button. I’m referencing Super Clone Pro pages that use different configurations for cloning in this example. This could easily be adapted for other pages or actions. Enjoy!


var $j = jQuery.noConflict(); 
$j(function() {
      var winHead = '' +
      var winBody = '';

        autoOpen: true, 
        modal: true,
        width: 500,
        buttons: { 
            "Cancel":  function() {$j(this).dialog("close");}

      $j( "#clone-op" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpclone?rid={!Account.Id}&cfg=StandardClone';
      $j( "#copy-from-op1" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpclone?rid={!Account.Id}&cfg=CloneOpt1';
      $j( "#copy-from-op2" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpclone?rid={!Account.Id}&cfg=CloneOpt2';
      $j( "#copy-from-op3" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpclone?rid={!Account.Id}&cfg=CloneOpt3';
      $j( "#copy-to-op1" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpcopy?rid={!Account.Id}&cfg=CopyOpt1';
      $j( "#copy-to-op2" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpcopy?rid={!Account.Id}&cfg=CopyOpt2';
      $j( "#copy-to-op3" ).button().on( "click", function() {
        window.location.href = '/apex/lcrm-scp__scpcopy?rid={!Account.Id}&cfg=CopyOpt3';


Alert with Three Options – jQueryUI in a Salesforce Custom Button

18 Aug

Recently, I was asked to create a custom button that would allow a user to choose between two SuperClone Configuration options. jQueryUI provides a nice modal dialog that solved the problem. The dialog showed two buttons for the configurations and a cancel button. Below is the sample code for what I put together. The actions for each button could be swapped out to load other any other pages or run javascript methods.

A few stackoverflow posts helped me put this together. Credit to:
jQueryUI Modal Dialog


var $g = jQuery.noConflict(); 
$g(function() {
        autoOpen: true, 
        modal: true, 
        buttons: { 
            "Config 1": function() {"superclone url here for config 1","_self");},
            "Config 2":  function() {"superclone url here for config 2","_self");},
            "Cancel":  function() {$g(this).dialog("close");}

Using the Enter Keypress Event in Visualforce to Search or Change Checkbox Properties

19 Dec

Perform common actions like run a search or check a checkbox when the enter key is pressed, instead of form submission. The example below shows how to do this with help from jQuery in the Visualforce page.

Things to notice:

  • jQuery.noConflict() prevents conflicts with other javascript that Salesforce may have loaded.
  • j$(document).ready will run the contained JavaScript after the components are loaded on the page. Binding event handling to elements could be missed without this.
  • event.keyCode == 13 is the key code for the Enter button.
  • CSS classes are used to attach the keypress event handling, but you could also specify element types, names, or any other valid selector logic.
  • searchFilter CSS class could be used on text, drop down/picklists, checkboxes, etc.
  • .trigger(‘click’) is used below. It is similar to .click(), and they are interchangeable for this example. The difference that I’m aware of is .trigger(‘click’) can accept additional parameters.
  • Probably only one or two of the methods below would be used in a real project. Multiple options are documented to illustrate different options.

Continue reading

Javascript Popup Window Tips for Salesforce

15 Sep

Check if a Popup Blocker Prevented the Popup Window

Most browsers will block popup windows the first time a popup window tries to appear. Here is how to check if a popup window was blocked.

Modal Window

Making a popup window modal helps prevent it from being lost behind the main browser window. The following technique makes the popup window display above the window that created it. However, it doesn’t force the window to stay on top when other tabs or applications have focus.

Refresh or Change the Original Page’s Location

A popup window may be used to alter information related to what is displayed on the page that initiated the window. This may require the original page to be refreshed or redirected. Assign the URL to the window.opener.location.href property to change opener’s page.

// change the location of the page that opened the popup
window.opener.location.href = "/apex/NewVfPage";
// refresh the page that opened the popup with the reload method 
window.reload(true);  // true=get page from the server, false=get page from cache

// Or, setting the location to itself
window.opener.location.href = window.opener.location.href;

Custom Button to Refresh the Original Page

Custom Buttons that use Javascript can do a lot. However, security in modern browsers can make it tricky to refresh or change the URL of the originating page. One way to do this is to have the originating page monitor the popup window that was created. The page can then refresh itself when the popup window no longer exists in memory.

Custom button code:

var win ="/apex/PopupWindowVfPage", "MyWinName", "height=500,width=500"); 

// check if the window exists every second
// refresh the window using the window's same location
var timer = setInterval(function() {
  if(win.closed) { 
    window.location.href = window.location.href; 
}, 1000);

Change the Window Size

Changing the window height to adapt to content within the page can be frustrating because of variations in different browsers. Below is an example of one way to do it. I would not call it pretty, but it works well enough. First, it sets an initial size for the page. This is to specify the width, and the content will flow down the page based on the width. Second, we set the page size again because we can now tell the height based on the new width. Please comment if you’ve found a better light weight way to do this.

Javascript in the Popup Window:

// attach the ChangeWindowSize call to the page's onload event
if (window.addEventListener) {
  window.addEventListener('load', ChangeWindowSize, false);
} else if (window.attachEvent) {
  window.attachEvent('onload', ChangeWindowSize);
function ChangeWindowSize() {
  // first adjust to set the width
  // resize a second time after width is set from the first resizing, 10th of a second later
  setTimeout(function(){ AdjustWindow(); }, 100);
function AdjustWindow() {
  var body = document.body;
  var height = Math.max(body.offsetHeight);
  // setting the width to 1200px and the height with a 95px buffer
  window.resizeTo(1200, height + 95);

Close Popup after a Successful Action

A popup window may ask for user input and automatically close when processing has completed successfully. The controller can set a Javascript Boolean variable on success, and the portion of the form that is rerendered can have a script assign a true/false value. Then a method that evaluates if the window should be closed can check the value of that variable.



Mozilla - Window open
Mozilla - setInterval
Bob Buzzard - VisualForce Lookup