Currently, I'm just using this line: opp.LeadSource = 'Web'; as a static value to set the opp.LeadSource picklist on my vf page. Is there any way to do it dynamically? Instead of using static, I hope I can get whatever default value set on LeadSource, like: opp.LeadSource = (get any default value assigned to lead source). Any way how to do it?please...
-
Possible duplicate of Dynamically Determine Calling Context? – Pranay Jaiswal Sep 10 '18 at 14:34
Check this knowledge article "Get the default value for a picklist in Apex".
Description: When we create a record using Apex, the default values of picklist fields will not be populated until DML operation have occurred for the record. It will be empty after being instantiated.
Resolution: Use the sample code below to get the default value so you can show it/use it.
String defaultVal;
Schema.DescribeFieldResult F = Account.SamplePicklist__c.getDescribe();
List <Schema.PicklistEntry> pickVals = F.getPicklistValues();
for (Schema.PicklistEntry pv: pickVals) {
if (pv.isDefaultValue()) {
defaultVal = pv.getValue();
}
}