Tag Archives: Javascript

Calling apex method from a custom button using javascript

Sometimes we need to call a method of an apex class, on click of a custom button. Let’s take an example, how we call an apex method, on click of a custom button.
Step 1:
Lets first create the Apex class.

global class MyClass {
    webservice static String myMethod(String studentId){
       return 'Test';


  • Class needs to be a Global class.
  • And the method you intend to call from the javascript must be a Webservice Method.

Step 2:
Now to setup the custom Button.
Goto –> Setup –> Object –> Buttons, links and Actions section –> Click New Button or Link


  • Enter the Name of the button.
  • Behaviour : Execute Javascript.
  • Content source : On-Click Javascript.

In the code for button copy the following line of codes:

  var studentId = '{!Student__c.Id}';
  var result = sforce.apex.execute("MyClass", "myMethod",{studentId : studentId}); 
catch(ex) {


Salesforce – Field Update on Button Click Using Javascript

Here in below example, there is a custom field Active__c in Contact object. On a button click want to update Active__c field status to true.

if(confirm("Do you want to active this Contact?") == true) 
    var objC = new sforce.SObject("Contact");        
        objC.Id = '{!Contact.Id}';
        objC.Active__c = true;
        var result = sforce.connection.update([objC]); 

Custom Clone Button in Salesforce

Salesforce provides Clone functionality for some standard objects(Standard Clone button), However some standard objects do not have this button. For this purpose of cloning we will need to create custom button that will perform the functionality of cloning.

This cloning functionality can be achieved by writing a javascript for this custom button.

As an example lets create a custom button “Clone” on Account object that will clone the record.

Simply override your custom button “Clone” with the following javascript and you will have your custom Clone button that functions exactly like standard clone button


Note: retUrl specifies the location where you want to be on press of back button.

How to disable right click on visualforce page?

Sometimes we need to disable right click on visualforce page. Here in this article i will demonstrate how to disable right click on visualforce page using javascript.

Let’s take a simple example:
The below visualforce page is a simple entry page of “Student” custom object. Here the javascript method “RightClickDisabled”, disabled right click in form onmousedown event.

<apex:page standardcontroller="Student__c">
      function RightClickDisabled(event){    
        if (event.button==2)
            alert("Right click is not allowed");       
  <apex:form onmousedown="RightClickDisabled(event)">
      <apex:pageblock title="Create Student">
              <apex:commandbutton action="{!save}" value="Save">
          <apex:pageblocksection columns="1">
              <apex:inputfield value="{!Student__c.First_Name__c}">
              <apex:inputfield value="{!Student__c.Last_Name__c}">
              <apex:inputfield value="{!Student__c.Date_of_Birth__c}">
              <apex:inputfield value="{!Student__c.Address__c}">              


download (1)

Calling Visualforce Page From Javascript

Visualforce page that calls another Visualforce page upon confirming from javascript.

Visualforce Page:

<apex:page standardController="Account">
            <apex:commandButton value="Open VF Page" onclick="OpenVFPage()"/>
      function OpenVFPage(){
        var isConfirm = confirm('Do you want to open a new Visualforce Page?');

Similarly, if you want to open a visualforce page from a custom button using javascript then use following piece of code.

var isConfirm = confirm('Do you want to open new Visualforce Page?');