How to get default value in a picklist in Salesforce?

How to get default value in a picklist in Salesforce?

When we insert a record using Apex, the default values of picklist fields will not be populated until DML operation is done on the record. It will be empty/blank. So, make sure that we use the below sample code for populating default values of picklist fields. It doesn’t count against SOQL limits, since we are getting it using schema description.


Sample Code:

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




Outputs:


Cheers!!!

Leave a Reply