Let’s say you coded a custom view or report using Visualforce/Apex and you want to give the user control over the reporting interval. One way to go would be a custom drop down menu. This example demonstrates how to create such a menu in Visualforce and tie the user’s selection to methods in the controller class (in this case, the reporting interval logic.)
Let’s start by defining a method in the page’s controller class to hold the menu values.
public List getPeriods() {
List periods= new List();
periods.add(new SelectOption(’Weekly’,'Weekly’));
periods.add(new SelectOption(’Quarterly’,'Quarterly’));
return periods;
}
Notice “SelectOption” in the code above. This is a Salesforce type specifically for drop down lists.

In this example, we’ve also added a “commandButton” component and gave it the value of “Submit”. When the user clicks it, the controller runs our reporting logic. The user’s selection is available within the controller via the simple method below:
// notice this method is called in the Visualforce markup without the “get”
public String getPeriod() {
return this.period;
}
Our Visualforce page will know what to display based on this line of code within the Visualforce markup:
<apex:pageBlockTable value=”{!weeklyRows}” var=”rows” title=”Your Weekly Report” rendered=”{!RenderedWeekly}”>
The value of the “rendered” attribute will be the return value of the getRenderedWeekly method. It will tell Visualforce whether or not to display the pageBlockTable. If it is true, getWeeklyRows will return the actual data.
Boolean userSelection = FALSE;
if(period == ‘Weekly’){
userSelection = TRUE;
}
return userSelection;
}
In Visualforce, components like selectList, selectOptions, pageBlockTable, etc.. have a value attribute that corresponds to a method in the page’s controller. These methods are prefixed with a “get” in the controller. (ex. value=”{!periods}” corresponds to controller method “getPeriods”)
Some Visualforce code shown with SFDC’s inline editor:

Hopefully this example helps to better understand the interaction between the user and a Visualforce page and its controller (an Apex class.)
Thanks to Brian Markey for showing me the drop down menu technique.
Happy cloud computing ~ ~
Related posts:
- Visualforce - Salesforce.com’s markup language Visualforce is SFDC’s custom mark up language and represents the...
- Creating a custom bulk edit screen Salesforce gives you some native bulk edit capabilities right out...
- How to avoid governor limits with sendEmail in Apex You can send emails programmatically in Apex, but if you’re...
- Adding a custom wizard feature in Salesforce Wizards generally consist of a user interface with step-by-step instructions....
- Setting Lead Assignment Rules with Apex If you want to create a new Lead via Visualforce...
Related posts brought to you by Yet Another Related Posts Plugin.










Posted by tburre on July 7, 2009 at 11:48 pm
