Displaying default picklist value on Visualforce page using apex class controller
Salesforce-Developer
Here, we do not need to use SOQL and thus eliminates the Salesforce Governer limitations.

Check the default checkbox against the value which needs to be defaulted
Steps to make the default value in Lead Object field called Status
Setup -> Lead (Object) -> Click Status field -> check the default checkbox against the value from Status value lists.

I have chosen – “Open – Not Contacted”.

Apex Class:

public class defaultPicklistController {

public Lead lead {
get;
set;
}
public List < SelectOption > statusOptions {
get;
set;
}
// Constructor called on load.
public picklistController() {
lead = new Lead();
statusOptions = new List < SelectOption > ();

// Use DescribeFieldResult object.
Schema.DescribeFieldResult statusFieldDescription = Schema.Lead.Status.getDescribe();
// For each picklist value, a new select option created
for (Schema.Picklistentry picklistEntry: statusFieldDescription.getPicklistValues()) {
statusOptions.add(new SelectOption(pickListEntry.getValue(), pickListEntry.getLabel()));
// check default value and assign
if (picklistEntry.isDefaultValue()) {
lead.Status = picklistEntry.getValue();
}
}
}
}

Visualforce Page:

<apex:page controller="defaultPicklistController">
<apex:form >
<b>Default value of Lead Status : </b>
<apex:selectList size="1" value="{!lead.Status}">
<apex:selectOptions value="{!statusOptions}"/>
</apex:selectList>
</apex:form>
</apex:page>

Output:

sfdc-development-lead-status

Written by 

Salesforce MVP | 15x Salesforce Certified | Lightning Champion | Application Architect | 6x Trailhead Ranger | Sydney | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*