function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajeevlsHydRajeevlsHyd 

Set default value for a picklist field in VF page

I want to set a defalut value of a picklist field in VF page.. Please suggest how to acheive?

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi Rajeev,

 

To set default value to picklist you have to set variable value in your constructor.

see Below Example.

 

VF Page:

<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!strSelCountry}" size="0">
            <apex:selectOptions value="{!Countries}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

 

Apex Class:

public class sampleCon{
    public string strSelCountry {get; set;}
    public List<SelectOption> lstCountries {get; set;}
    
    public sampleCon(){
        strSelCountry = 'MEXICO';
    }
    public List<SelectOption> getCountries() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    }   
}

 

 

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

All Answers

hitesh90hitesh90

Hi Rajeev,

 

To set default value to picklist you have to set variable value in your constructor.

see Below Example.

 

VF Page:

<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!strSelCountry}" size="0">
            <apex:selectOptions value="{!Countries}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

 

Apex Class:

public class sampleCon{
    public string strSelCountry {get; set;}
    public List<SelectOption> lstCountries {get; set;}
    
    public sampleCon(){
        strSelCountry = 'MEXICO';
    }
    public List<SelectOption> getCountries() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('US','US'));
        options.add(new SelectOption('CANADA','Canada'));
        options.add(new SelectOption('MEXICO','Mexico'));
        return options;
    }   
}

 

 

 

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Sales cloud consultant
My Blog:- http://mrjavascript.blogspot.in/

This was selected as the best answer
jodev191.395407754397709E12jodev191.395407754397709E12
Thanks a lot....its work for me....

Nash79Nash79
Hi Hitesh, Thank you for your post. Why do we need the below variable in the above code?

public List<SelectOption> lstCountries {get; set;}

Thanks, Nagesh M