Picklists provide options to the user to select any value which is applicable for that field. As in the standard page layout picklist will provide those kind of options.
Sometimes we need to retrive the values of the picklist to display them on our visualforce pages to provide the user to select the applicable option, because some picklists restricts to enter some values those are not specified in its design.
To get the values of the picklist just use the below piece of code in your apex class,
To populate those values on visualforce page just place below code in your page,
Sometimes we need to retrive the values of the picklist to display them on our visualforce pages to provide the user to select the applicable option, because some picklists restricts to enter some values those are not specified in its design.
To get the values of the picklist just use the below piece of code in your apex class,
public List<SelectOption> getPicklistOptions()
{
List<SelectOption> options = new List<SelectOption>();
Schema.DescribeFieldResult fieldResult =
Opportunity.stagename.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for( Schema.PicklistEntry f : ple)
{
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
return options;
}
The above piece of code retrives the options of the picklist stagename of the standard opportunity object, just replace the name of your sObject and picklist to retrieve those values in your apex class.
<apex:selectList id="picklist1" value="{!Opportunity.stagename}"
size="1" required="true">
<apex:selectOptions value="{!PicklistOptions}"/>
</apex:selectList>
Where apex is the name of the default namespace used in visualforce page.