Newer Version Available
Defining Action Methods
- <apex:commandButton> creates a button that calls an action
- <apex:commandLink> creates a link that calls an action
- <apex:actionPoller> periodically calls an action
- <apex:actionSupport> makes an event (such as “onclick”, “onmouseover”, and so on) on another, named component, call an action
- <apex:actionFunction> defines a new JavaScript function that calls an action
- <apex:page> calls an action when the page is loaded
For example, in the sample page described in Using Input Components in a Page, a command button is bound to the save method in the Account standard controller. We can adapt that previous example so that it now uses the MyController custom controller:
After saving the page above, the Visualforce editor offers a “quick fix” option to add the save method to the MyController class. If you click the quick fix link, MyController now looks like this:
The save method that is generated by the quick fix takes the standard signature for an action method: it is public, returns a PageReference, and contains no arguments.
- The member variable must be added to the class
- The member variable must be set when getAccount performs the initial query
Now that the member variable is in place, all that the save method needs to do is update the database:
A more robust solution for save might catch various exceptions, look for duplicates, and so on. Since this is meant to be a simple example, those details have been left out.
To test this page, change the value in the Change Account Name field and click Save New Account Name. As with the standard Account controller example, the page simply refreshes with the new account name. In the next example, we will extend the save action so that instead of refreshing the current page, it navigates the user to a different confirmation page.