You need to sign in to do that
Don't have an account?
❤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
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
you can assign default value in your constructor
Thanks,
Alex
All Answers
You can assign default value for discountScheduleID property.
for example paste this code in your controller
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
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
you need get list of options before, try this
Thanks,
Alex
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
you can assign default value in your constructor
Thanks,
Alex
try this
Thanks,
Alex
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();
}
}