
Setting a value into a controller property
Visualforce controllers are often re-used across pages with minor variations in behavior specific to the page, for example, displaying accounts of a particular type. While the controller can detect the page that it is being used by and alter its behavior accordingly, this is not a particularly maintainable solution, as use of the controller in any new page would require changes to the Apex code and renaming a page would break the functionality.
A better mechanism is for the page to set the values of properties in the controller to indicate the desired behavior. In this recipe we will create a custom component that takes two attributes: a value and the controller property to set the value into. Two Visualforce pages with a common controller will also be created to demonstrate how the component can be used to change the behavior of the controller to suit the page.
Getting ready
This recipe does not require any Apex controllers, so we can start with the custom component.
How to do it…
- Navigate to the Visualforce Components setup page by clicking on Your Name | Setup | Develop | Components.
- Click on the New button.
- Enter
SetControllerProperty
in the Label field. - Accept the default SetControllerProperty that is automatically generated for the Name field.
- Paste the contents of the
SetControllerProperty.component
file from the code download into the Visualforce Markup area and click on the Save button. - Next, create the custom controller for the Visualforce pages by navigating to the Apex Classes setup page by clicking on Your Name | Setup | Develop | Apex Classes.
- Click on the New button.
- Paste the contents of the
AccountsTypeController.cls
Apex class from the code download into the Apex Class area. - Click on the Save button.
- Next, create the first Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
- Click on the New button.
- Enter
AccountsType1
in the Label field. - Accept the default AccountsType1 that is automatically generated for the Name field.
- Paste the contents of the
AccountsType1.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 AccountsType1 page and click on the Security link.
- On the resulting page, select which profiles should have access and click on the Save button.
- Finally, create the second Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop | Pages.
- Click on the New button.
- Enter
AccountsType2
in the Label field. - Accept the default AccountsType2 that is automatically generated for the Name field.
- Paste the contents of the
AccountsType2.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 AccountsType2 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 first Visualforce page in your browser displays a list of accounts whose type is Customer - Direct: https://<instance>/apex/AccountsType1
.
Here, <instance>
is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

While, opening the second Visualforce page displays a list of accounts whose type is Customer - Channel: https://<instance>/apex/AccountsType2
.
Here, <instance>
is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

The SetControllerProperty
custom component assigns the value attribute to the controller property attribute:
<apex:component > <apex:attribute name="from" type="String" assignTo="{!to}" description="The value to set"/> <apex:attribute name="to" type="String" description="The controller property to set the value into"/> </apex:component>
The Visualforce pages set the type of account to be retrieved into the controller property via the custom component.
<c:SetControllerProperty from="Customer - Direct" to="{!accType}" />
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.
- The Passing attributes to components recipe in this chapter shows how an sObject may be passed as an attribute to a custom component.