Controller:
public class Sample{ public Account acc {get;set;} public List<SelectOption> typeOptions {get;set;} // Constructor called when page is accessed. public Sample() { acc = new Account(); typeOptions = new List<SelectOption>(); //Use DescribeFieldResult object to retrieve status field. Schema.DescribeFieldResult typeFieldDescription = Account.Type.getDescribe(); //For each picklist value, create a new select option for (Schema.PickListEntry pl:typeFieldDescription.getPicklistValues()){ typeOptions.add(new SelectOption(pl.getValue(),pl.getLabel())); //Obtain and assign default value if (pl.defaultValue){ acc.Type= pl.getValue(); } } } }
Visualforce Page:
<apex:page controller="Sample"> Please select account type: <br/> <apex:form > <apex:selectList size="1" value="{!acc.Type}"> <apex:selectOptions value="{!typeOptions}"/> </apex:selectList> </apex:form> </apex:page>