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}”/>
</apex:selectList>
</apex:form>
</apex:page>

(1668)

Written by 

I am Sakthivel Madesh, a certified Salesforce Developer & Administrator working on Salesforce Technology since 2011. I am currently working in IBM Austraila as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 10 Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply