[SalesForce] how to display default value in the vf page using apex;inputfield

Is it possible to display the default value of the pick list in object in visual Force page using apex:inputfield ?

Best Answer

In order to do that you need to make describe call and populate the selected value with it.

Lets assume you are holding value of selected country in selectedCountry varible then you need to call below method from constructor in order to show default picklist value for country picklist. Code snippet:

public void selectDeafultCountry()
{
  List<SelectOption> options = new List<SelectOption>();

   Schema.DescribeFieldResult fieldResult =
        OfficeLocation__c.Country__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

   for( Schema.PicklistEntry f : ple)
   {
      if(f.isDefaultValue()){
        selectedCountry = f.getValue();break;
      }
   }       
   return options;
}

Ref: https://developer.salesforce.com/blogs/developer-relations/2008/12/using-the-metadata-api-to-retrieve-picklist-values.html

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Schema_PicklistEntry.htm