Creating a button to Call an Apex Class

You can create a button which will call an apex class in the background. At the same time you can open a visual force page on click of that button and display the information you processed with the apex class.

There are three simple steps you need to follow:

1) Create an Apex class
2) Create a Visual Force page
3) Create a button

Lets take a simple example. On click of account detail page button, we will open a Visual Force page which will display the name of the Account.

Creating an Apex Class

public class showaccount{

Account acc;
Account acct;

public showaccount(ApexPages.StandardController stdController){
this.acc = (Account)stdController.getRecord();
acct = [Select Name From Account Where Id=:acc.Id];

public String getShowAccountName(){
return acct.Name;

Creating Visual Force Page

<apex:page standardController="Account" extensions="showaccount">
<apex:outputText > {!ShowAccountName} </apex:outputText>


Creating Button

Customize -> Account -> Buttons and Links -> New

Add this to the account page layout and click the button to open the visual force page to see


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s