I think I am missing something basic. I have two select lists: Region and District. Based on region selection, the district values should get updated.
Following is the code for VF:
<apex:page Controller="UserStatsController">
<apex:sectionHeader title="Goal Revision Dashboard" subtitle="District View"/>
<apex:pageBlock title="User Stats">
<apex:form >
<apex:outputLabel value="Region" />
<apex:selectList value="{!regn}" size="1" id="Region" required="true">
<apex:selectOptions value="{!Valuesr}" />
<apex:actionSupport event="onchange" reRender="districtSelect"/>
</apex:SelectList>
<apex:outputPanel id="districtSelect">
<apex:outputLabel value="District"/>
<apex:selectList value="{!dist}" size="1" id="dist" required="true">
<apex:selectOptions value="{!Values}"/>
</apex:selectList>
</apex:outputPanel>
</apex:page>
And following is the controller
public with sharing class UserStatsController {
public string regn {get; set;}
public string dist {get; set;}
public list <SelectOption> getValuesr()
{
list <selectoption> lsreg = new List<selectoption> ();
lsreg.add(new SelectOption('', '--None--'));
for (User_Stats__C Rgn : [SELECT Region__c FROM User_Stats__C where Region__c != null and Region__c != 0 and District__c != null and District__c != '0' limit 10])
{
lsreg.add(new selectoption(string.valueOf(Rgn.Region__c), string.valueOf(Rgn.Region__c)));
}
return lsreg;
}
public list <SelectOption> getValues()
{
list <selectoption> ls = new List<selectoption> ();
ls.add(new SelectOption('', '--None--'));
Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
for (User_Stats__C dist : [SELECT District__c FROM User_Stats__C where Region__c =: regnInt and District__c != '' and Region__c != null and Region__c != 0 limit 10])
{
ls.add(new selectoption(dist.District__c, dist.District__c));
}
return ls;
}
}
Thanks.
Best Answer
The error is coming from your query for dist, specifically the where clause:
'Regn' is null when the page loads. Additionally, there appears to be a problem with your page DOM. The way it is structured, I don't think you'll get your getters and setters working in tandem they way you want. I've refactored your code a bit. The VF may not be entirely correct for your case since we're only seeing a snippet. But with this refactor, you can leverage the setter for regn properly and rerender the dist picklist using actionsupport. Also, I've added in a blank initial value for regn and dist so that the user is forced to make a selection, thus firing the onchange event.
VF
Controller