Thursday, January 26, 2012

Visualforce Custom button

As Salesforce has deprecated the sControl and hence there is more need to build the custom button using visualforce. We will go through to designing of custom button using Visualforce and apex code.

Here I have given simple example

Step - 1 : Create Apex class with logic which needs to execute on click on custom button.

public with sharing class CustomButtonExample {
    private final ApexPages.StandardController theController;

    public CustomButtonExample (ApexPages.StandardController controller) {
        theController = controller;

    public PageReference doCheckLogic() {
     String theId = ApexPages.currentPage().getParameters().get('id');
            // do something
        catch(Exception e){
       return theController.view().setRedirect(true);

Step - 2 : Create Visualforce
   <apex:page action="{!doCheckLogic}" extensions="CustomButtonExample" 
            standardcontroller="Opportunity"> </apex:page>

Remember that the custom controller you build must be extensions 
in Visualforce page.

Step - 3 : Create Custom Button
Go to setup->App Setup-> Customize -> Custom Object -> Buttons and Links provide the parameters and select the Visualforce create from the list.

Step - 4 : Put Custom button on page layout.

You are done !!