+ Start a Discussion
❤Code❤Code 

How to display default value on VF Page load in Picklist

Hi All,

I hv a vf page where i need to populate default value as ' United States' on [page load and also geography field should get all the regions related to United States.

Below is my VF page & controller method.

Controller -

public List<SelectOption> getDept()
        {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('Select','--Select--'));
            Schema.DescribeFieldResult departfield =  Geography__c.Country__c.getDescribe();
            List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
            for( Schema.PicklistEntry f : picklist){      
            options.add(new SelectOption(f.getLabel(), f.getValue()));
     
            }    
            return options;
            
         }

VF Page - 

<apex:form id="form">
        <apex:pageMessages />
        <apex:selectList value="{!discountScheduleID}" size="1" styleClass="fullWidth chzn-select">
            <apex:actionSupport event="onchange" action="{!FindAllName}" rerender="geographies" oncomplete="renderChosen()"/>
            <apex:selectOptions value="{!dept}" />
        </apex:selectList>  

          <apex:outputLabel value="{!$ObjectType.RFP__c.Fields.Geography__c.Label}" />
              <apex:outputPanel id="geographies" layout="block"  styleClass="requiredInput">
           <apex:outputPanel id="geos" layout="block"  styleClass="requiredBlock"/>
         <apex:selectList value="{!selectedGeographyIds }" multiselect="true" id="selectedGeographies" styleClass="fullWidth chzn-select" >
            <apex:selectOptions value="{!AllName}" id="movieTextBox"></apex:selectOptions>
        </apex:selectList>
        <apex:outputPanel layout="block" styleClass="errorMsg" >
                            <apex:outputText value="{!errorMap['selectedGeographies']}" 
                                escape="false" rendered="{!errorMap['selectedGeographies'] != ''}" />
                        </apex:outputPanel>
                    </apex:outputPanel>

    </apex:form>

Regards
Best Answer chosen by ❤Code
Alexander TsitsuraAlexander Tsitsura
Oh, in this case you write recirsion method.

you can assign default value in your constructor
 
public List<SelectOption> getDept()
{
	List<SelectOption> options = new List<SelectOption>();
	options.add(new SelectOption('Select','--Select--'));
	Schema.DescribeFieldResult departfield =  Geography__c.Country__c.getDescribe();
	List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
	for( Schema.PicklistEntry f : picklist){      
	options.add(new SelectOption(f.getLabel(), f.getValue()));

	}    
	return options;
 }
 
// your constructor, if not exists create it
public <Put here class name>() {
	// existing code
	
	List<SelectOption> options = getDept();
	if (!options.isEmpty()) {
		discountScheduleID = options[0].getValue();
	}
}


Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi,

You can assign default value for discountScheduleID property.

for example paste this code in your controller
discountScheduleID = dept[0].getValue();



As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
❤Code❤Code
HI Alexander,

I m getting the below error - Can u please check

Error: RfpControllerExtension2 Compile Error: Variable does not exist: dept at line 33 column 22

Regards
Alexander TsitsuraAlexander Tsitsura
Oh, sorry

you need get list of options before, try this
 
List<SelectOption> options = getDept();
if (!options.isEmpty()) {
    discountScheduleID = options[0].getValue();
}


Thanks,
Alex
 
❤Code❤Code
Hi Alexander,

I tried changing the method like this below - I am getting error 

System.LimitException: Maximum stack depth reached: 1001 
Class.RfpControllerExtension2.getDept: line 47, column 1

public List<SelectOption> getDept()
    {
        List<SelectOption> options = new List<SelectOption>();
        //options.add(new SelectOption('Select','--Select--'));
        options = getDept();
        if (!options.isEmpty()) {
        discountScheduleID = options[0].getValue();
        }
        Schema.DescribeFieldResult departfield =  Geography__c.Country__c.getDescribe();
        List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
        for( Schema.PicklistEntry f : picklist){   
         
        options.add(new SelectOption(f.getLabel(), f.getValue()));
 
        }    
        return options;
        
     }

Regards
Alexander TsitsuraAlexander Tsitsura
Oh, in this case you write recirsion method.

you can assign default value in your constructor
 
public List<SelectOption> getDept()
{
	List<SelectOption> options = new List<SelectOption>();
	options.add(new SelectOption('Select','--Select--'));
	Schema.DescribeFieldResult departfield =  Geography__c.Country__c.getDescribe();
	List<Schema.PicklistEntry> picklist = departfield.getPicklistValues();
	for( Schema.PicklistEntry f : picklist){      
	options.add(new SelectOption(f.getLabel(), f.getValue()));

	}    
	return options;
 }
 
// your constructor, if not exists create it
public <Put here class name>() {
	// existing code
	
	List<SelectOption> options = getDept();
	if (!options.isEmpty()) {
		discountScheduleID = options[0].getValue();
	}
}


Thanks,
Alex
This was selected as the best answer
❤Code❤Code
Alexander.. The above code will get the [0] index value.. But i want to set the default value in picklist on page load. Can u plz check and help..
Alexander TsitsuraAlexander Tsitsura
yes, you can write default value into property

try this
// your constructor, if not exists create it
public <Put here class name>() {
	// existing code
	
	
	discountScheduleID = 'My default value';
}


Thanks,
Alex
 
❤Code❤Code
Got the answer. Thnx fpr the help..I hv used the below code - 

Schema.DescribeFieldResult F = Geography__c.Country__c.getDescribe();
List <Schema.PicklistEntry> pickVals = F.getPicklistValues();        
for (Schema.PicklistEntry pv: pickVals) {
    if (pv.isDefaultValue()) {
        discountScheduleID  = pv.getValue();
    }    
}