Salesforce Picklist example Visualforce page

Picklist Examples




Example 1: Getting all names of an account in to a Picklist
page:
<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>




class:
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;
}
}



Example 2: Dependent Picklist Preparation using Controller:
In this scenario we can see how to generate a picklist dynamically and preparation of dependant picklists.
Here dependant picklist is will be populated using apex:actionsupport.
page:
<apex:page sidebar=”false” controller=”DependentClassabcd”>
<apex:form >
Departments: <apex:selectList value=”{!selDept}” multiselect=”false” size=”1″>
<apex:actionSupport event=”onchange” reRender=”out”/>
<apex:selectOptions value=”{!deptnames}”>
</apex:selectOptions>
</apex:selectList> <br/>
Employees: <apex:selectList multiselect=”false” id=”out” size=”1″>
<apex:selectOptions value=”{!empnames}”>
</apex:selectOptions>
</apex:selectList>
</apex:form>
</apex:page>

class:
public with sharing class DependentClassabcd {
public String selDept { get; set; }
List<selectOption> empOptions = new List<selectOption>();
public List<selectOption> getEmpnames() {
empOptions.clear();
empOptions.add(new selectOption(‘–None–‘,’–None–‘));
for(Employee__c objEmp : [select Id,name,department__c from Employee__c where department__r.name=:selDept]){
empOptions.add(new selectOption(objEmp.name,objEmp.name));
}
return empOptions;
}
List<selectOption> deptOptions = new List<selectOption>();
public List<selectOption> getDeptnames() {
deptOptions.add(new selectOption(‘–None–‘,’–None–‘));
for(Department__c objDept : [select name from Department__c]){
deptOptions.add(new selectOption(objDept.name,objDept.name));
}
return deptOptions;
}
}








Leave a Reply