A whole new level of customization with Custom Buttons

April 15, 2015

One of the various behaviors of Custom Button is "Execute JavaScript", with a content source of OnClick JavaScript.

They can not only execute javascript Functions On Click, but you can make Javascript execute on page load too.

The main use for this being complete control over every element in the object detail page without user clicking a button. We use jQuery for this.

We begin by importing the required jQuery files.


Optionally you have to import connection.js and apex.js if you want to perform any operations using sforce.


Below is the jQuery code which hides a button and triggers the click of a button.

var j$ = jQuery.noConflict();

Replace btn_Name with the name of the custom button you just created. Now, to make this code run on Page Load, we have to encode it into Base64, and add it to the REQUIRESCRIPT tag.

Use the URL http://rot47.net/base64encoder.html to encode the jQuery code above. This will produce the below result.


We then use the below statement to embed this encoded content :

{!REQUIRESCRIPT('data:application/javascript;base64,**ENCODED STRING**')}

After this, you can write the Javascript code you want to execute on Load.

The below example displays an alert on page load.

alert('Hi {!$User.FirstName}');

Since, the custom button is present in the HTML Content of the Object Detail page, you can use jQuery to select any element of the entire page and customize it.

This will work in all places where SF Buttons are shown. So it won't work in Salesforce1.