
Passing action methods to components
A controller action method is usually invoked from the Visualforce page that it is providing the logic for. However, there are times when it is useful to be able to execute a page controller action method directly from a custom component contained within the page. One example is for styling reasons, in order to locate the command button that executes the action method inside the markup generated by the component.
In this recipe we will create a custom component that provides contact edit functionality, including command buttons to save or cancel the edit, and a Visualforce page to contain the component and supply the action methods that are executed when the buttons are clicked.
How to do it…
This recipe does not require any Apex controllers, so we can start with the custom component.
- Navigate to the Visualforce Components setup page by clicking on Your Name | Setup | Develop | Components.
- Click on the New button.
- Enter
ContactEdit
in the Label field. - Accept the default ContactEdit that is automatically generated for the Name field.
- Paste the contents of the
ContactEdit.component
file from the code download into the Visualforce Markup area and click on the Save button. - Next, create the Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
- Click on the New button.
- Enter
ContactEditActions
in the Label field. - Accept the default ContactEditActions that is automatically generated for the Name field.
- Paste the contents of the
ContactEditActions.page
file from the code download into the Visualforce Markup area and click on the Save button. - Navigate to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
- Locate the entry for the
ContactEditActions
page and click on the Security link. - On the resulting page, select which profiles should have access and click on the Save button.
How it works…
Opening the following URL in your browser displays the ContactEditActions page: https://<instance>/apex/ContactEditActions?id=<contact_id>
.
Here, <instance>
is the Salesforce instance specific to your organization, for example, na6.salesforce.com, and <contact_id>
is the ID of any contact in your Salesforce instance.

The Visualforce page simply includes the custom component, and passes the Save
and Cancel
methods from the standard controller as attributes.
<apex:page standardController="Contact"> <apex:pageMessages /> <apex:form > <c:ContactEdit contact="{!contact}" saveAction="{!save}" cancelAction="{!cancel}" /> </apex:form> </apex:page>
The ContactEdit
custom component declares attributes for the action methods of type ApexPages.Action
.
<apex:attribute name="SaveAction" description="The save action method from the page controller" type="ApexPages.Action" required="true"/> <apex:attribute name="CancelAction" description="The cancel action method from the page controller" type="ApexPages.Action" required="true"/>
These attributes can then be bound to the command buttons in the component in the same way as if they were supplied by the component's controller.
<apex:commandButton value="Save" action="{!SaveAction}" /> <apex:commandButton value="Cancel" action="{!CancelAction}" immediate="true" />
There's more…
While this example has used action methods from a standard controller, any action method can be passed to a component using this mechanism, including methods from a custom controller or controller extension.
See also
- The Updating attributes in component controllers recipe in this chapter shows how a custom component can update an attribute that is a property of the enclosing page controller.