You need to sign in to do that
Don't have an account?
Set selectList Default Value based on Record Value
I have set up a VF page to replace the edit page for a custom object “CompetencyOwner__c”. The object contains two fields “Region__c” and “Country__c” which I want to be able to let the user change with selectLists. I have no problem building the selectOption values and making the Country values dependent on the selected Region. My problem is that I don’t understand how to set the default value of the dropdowns based on the current record values of those fields.
I have a getter method to return the list of selectList values to populate the dropdowns. I also have getter and setter methods for the current value of the selection. Here’s an example for the Region field:
String selectedRegion; //for selected value
public List<selectOption> getRegions() {
List<SelectOption> regionOps=new List<SelectOption>();
List<Region__c> regionList=[Select Name from Region__c Order by Name];
for(Region__c r:regionList) {
regionOps.add(new SelectOption(r.ID,r.Name));
}
return regionOps;
}
public String getSelectedRegion() {
return SelectedRegion;
}
public void setSelectedRegion(String SelectedRegion) {
this.SelectedRegion=SelectedRegion;
}
Assume I can get the actual record to be edited. How do I set the initial value of the dropdown to the selectOption that corresponds to the record ID or Name?
public List<selectOption> getRegions() {
List<SelectOption> regionOps=new List<SelectOption>();
List<Region__c> regionList=[Select Name from Region__c Order by Name];
for(Region__c r:regionList) {
regionOps.add(new SelectOption(r.ID,r.Name));
if(selectedRegion == null)
selectedRegion = r.Name;
}
return regionOps;
}
public String getSelectedRegion() {
return SelectedRegion;
}
public void setSelectedRegion(String SelectedRegion) {
this.SelectedRegion=SelectedRegion;
}
All Answers
public List<selectOption> getRegions() {
List<SelectOption> regionOps=new List<SelectOption>();
List<Region__c> regionList=[Select Name from Region__c Order by Name];
for(Region__c r:regionList) {
regionOps.add(new SelectOption(r.ID,r.Name));
if(selectedRegion == null)
selectedRegion = r.Name;
}
return regionOps;
}
public String getSelectedRegion() {
return SelectedRegion;
}
public void setSelectedRegion(String SelectedRegion) {
this.SelectedRegion=SelectedRegion;
}
Thanks!