Calling an Apex method from a Button

This is also a common need in salesforce.com development. How to call an Apex method via a button press.
I thought I'd put a post explaining your options on how to achieve this.

Firstly, create a new VF controller extension, which extends the object on whose screen you'll need the button.

For example, you want a button on the Account page which triggers some custom apex, so go ahead and create a new controller called, AccountExtension, (this is a new Apex class):

public class AccountExtension {

// global class variable for the Account object that
// triggered this page view
private final Account a;

// constructor for controller extension
// takes the standard controller as input
public AccountExtension(ApexPages.StandardController stdController) {

// takes the Account object which spawned the page view
// retrieves the standard controller and casts it
// and assigns it to this page
this.a= (Account)stdController.getRecord();
}

public void myMethod {
// put your custom apex code here
// typically your would instantiate another object and execute it
// but a code block works just the same
}

public String getMethod {
// put your custom apex code here
// typically your would instantiate another object and execute it
// but a code block works just the same, in this case a String is returned
}

public void setMethod(String mystring) {
// put your custom apex code here
// typically your would instantiate another object and execute it
// but a code block works just the same, in this case a string must
// be passed to the method
}
}

Now once you have saved this, create a new VisualForce page, there are 3 ways you can initiate custom Apex via a visualforce page, each require the extension with the code.

Option 1 - Action Method
By following the below code, you can see as an attribute in the vf page, we have something called 'action'. The action attribute will execute whichever method it is provided on page load. In this case I've put the action in the page element, but it can be in other elements too.

<apex:page standardcontroller="Account" extensions="AccountExtension" action="{!mymethod}">
<apex:sectionheader title="My Page">
</apex:sectionheader>
</apex:page>

Option 2 - Getter Method
This approach is different to an action method, as it is executed in line with the rest of the vf page, and getter methods can return a value.

<apex:page standardcontroller="Account" extensions="AccountExtension">
<apex:sectionheader title="My Page">
{!Method}
</apex:sectionheader>
</apex:page>

Option 3 - Setter Method
Setter methods pass values from the UI to an apex method. It is also executed inline on the page.

<apex:page standardcontroller="Account" extensions="AccountExtension">
<apex:sectionheader title="My Page">
<apex:inputtext id="my lovely string value" value="{!Method}">
</apex:inputtext>
</apex:sectionheader>
</apex:page>

The tricky bit for me was the naming syntax. For example, to be a getter method, the name of method in the controller class must begin with get, and similarily with setter methods, it must begin with set, although for action methods, you can call it from it's full name.
Then just go ahead and create a new custom button on the Accounts page, which links to the Visualforce page you've just created, and voila!
You can read more about this here

1 comments:

Joel Dietz said...

No. 3 works w/o a apex:form element or a commandButton?

Post a Comment