This Visualforce page and its Apex controller class demonstrate how to display a dropdown list (also known as a picklist) of account names for selection in Salesforce. Here’s a beginner-friendly explanation:
Salesforce Picklist example
Visualforce Page Code
<apex:page sidebar="false" controller="accPickNamesDisplayClass">
<apex:form>
<apex:selectList multiselect="false" size="1">
<apex:selectOptions value="{!accNames}">
</apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>
Explanation:
<apex:page sidebar="false" controller="accPickNamesDisplayClass">:
- This defines a Visualforce page.
sidebar="false"removes the default Salesforce sidebar, creating a cleaner interface.controller="accPickNamesDisplayClass"links this page to a custom Apex class (accPickNamesDisplayClass), which will provide the data for the dropdown.
<apex:form>:
- This tag creates a form, which groups form elements together, like the dropdown list.
<apex:selectList multiselect="false" size="1">:
<apex:selectList>creates a dropdown list.multiselect="false"ensures users can select only one option from the list.size="1"defines the size of the dropdown. Setting this to “1” means the dropdown will show only one option at a time (a single-select list).
<apex:selectOptions value="{!accNames}">:
- This tag specifies the options that will appear in the dropdown.
value="{!accNames}"binds the dropdown options to theaccNamesproperty in the Apex controller, which provides the account names.
This Visualforce page will display a dropdown of account names, populated by the Apex class.
Apex Controller Code
public with sharing class accPickNamesDisplayClass {
List<selectOption> options = new List<selectOption>();
public List<selectOption> getAccNames() {
for(DataLoadTest__c acc : [select Id, name from DataLoadTest__c]) {
options.add(new selectOption(acc.name, acc.name));
}
return options;
}
}
Explanation:
public with sharing class accPickNamesDisplayClass:
- This defines a public Apex class named
accPickNamesDisplayClass. with sharingenforces the user’s data access permissions, ensuring the user only sees records they are permitted to access.
List<selectOption> options = new List<selectOption>();:
- This line creates a list named
optionsto store each option in the dropdown. - Each
selectOptionobject represents one option in the dropdown, containing a display label and a value.
public List<selectOption> getAccNames():
- This method,
getAccNames, provides the list of options for the dropdown in the Visualforce page. - Visualforce will call this method to retrieve the options for
accNames.
for(DataLoadTest__c acc : [select Id, name from DataLoadTest__c]):
- This
forloop retrieves records from the custom objectDataLoadTest__c(which can be replaced with any object, likeAccountorContact, depending on your need). - The loop fetches the
IdandNamefields of eachDataLoadTest__crecord.
options.add(new selectOption(acc.name, acc.name));:
- For each record retrieved, a new
selectOptionobject is created. new selectOption(acc.name, acc.name)creates an option where both the display label and the value are set to thenamefield of theDataLoadTest__crecord.- This option is then added to the
optionslist.
return options;:
- This method returns the
optionslist, which contains all theselectOptionobjects. - These options are displayed as the choices in the dropdown on the Visualforce page.
How It Works Together
- When the Visualforce page loads, it calls the
getAccNamesmethod in theaccPickNamesDisplayClass. - This method fetches all
DataLoadTest__crecords, creates options for each record, and returns them. - The options appear in the
<apex:selectList>dropdown, allowing users to select from a list of account names.
This simple setup is a powerful way to display dynamic dropdown lists in Salesforce, pulling data directly from the database with minimal code.