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

  • Yes, are you facing any challenges? – Raul Oct 11 '16 at 12:41
  • 1
    if you set default in field level in VF page inputfield will handle that. Are you getting any exception? – Tushar Sharma Oct 11 '16 at 12:48
  • no i am not getting any exception.can u explain what is meant by setting default value in the input field. – user36442 Oct 11 '16 at 13:00

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 =
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

   for( Schema.PicklistEntry f : ple)
        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


| improve this answer | |

To display default value, assign the default value from controller method.

Picklist__c = "default value";

| improve this answer | |
  • can u explain briefly. – user36442 Oct 11 '16 at 12:58

You need to get the default value from the constructor of controller: In Controller:

m_objShipMethod = [Select Id, ShippingMethod__c from Shipping_Method__c where ShippingMethod__c = 'FedEx Ground' limit 1];
m_objOrderObj = [Select Shipping_Method__c from Order__c where Shipping_Method__c = :m_objShipMethod.Id limit 1];

In the VF page:

<apex:inputField value="{!m_objOrderObj.Shipping_Method__c}" styleClass="textbox"/>

for more information please see the link

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.