How to get the default picklist value using Apex Class?

Its possible to get the default picklist value using apex class and doesn’t require the SOQL Query and its not against the SOQL Limits as well.
Apex Code Code:

String defaultVal;
Schema.DescribeFieldResult F = Lead.Status.getDescribe();
List <Schema.PicklistEntry> pickVals = F.getPicklistValues();
for (Schema.PicklistEntry av: pickVals) {
if (av.isDefaultValue()) {
defaultVal = av.getValue();
system.debug(‘Default value is’ + defaultVal);

isDefaultValue – using the keyword we can achieve the default picklist values using apex class/apex triggers.

How to get the list of picklist values in apex controller?

Apex Class:
public class ApexPicklistController{

public Lead lead{get;set;}
public List<SelectOption> statusOptions {get;set;}

// Constructor called when page is accessed.
public ApexPicklistController() {
lead = new Lead();
statusOptions = new List<SelectOption>();

// Use DescribeFieldResult object to retrieve status field.
Schema.DescribeFieldResult statusFieldDescription =  Lead. Status.getDescribe();
// For each picklist value, create a new select option
for (Schema.Picklistentry  picklistEntry: statusFieldDescription.getPicklistValues())
statusOptions.add(new SelectOption( pickListEntry.getValue(),pickListEntry.getLabel()));
// obtain and assign default value

list the collected picklist option to VF Page:
<apex:page controller=”ApexPicklistController”>
Please selectValue:
<apex:form >
<apex:selectList size=”1″ value=”{!led.Status}”>
<apex:selectOptions value=”{!statusOptions}”/>

Written by 

2x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

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