[SalesForce] VisualForce not able to render select list

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:

Region__c =: integer.valueof(apexpages.currentpage().getparameters().get('Regn'))

'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

<apex:page Controller="UserStatsController">
    <apex:sectionHeader title="Goal Revision Dashboard" subtitle="District View"/>
    <apex:form >
        <apex:pageBlock title="User Stats">
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Region" />
                    <apex:selectList value="{!regn}" size="1" id="region" required="true">
                        <apex:selectOptions value="{!valuesr}" />
                        <apex:actionSupport id="actionSupport" event="onchange" reRender="districtSelect"/>
                    </apex:SelectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockSection id="districtSelect">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="District"/>
                    <apex:selectList value="{!dist}" size="1" id="dist">
                        <apex:selectOptions value="{!Values}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

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--'));
        Set<String> regionSet = new Set<String>();
        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])
        {
            String region = string.valueOf(Rgn.Region__c);
            if(!regionSet.contains(region)) lsreg.add(new selectoption(region, region));
            regionSet.add(region);
        }
        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;
        Set<String> distSet = new Set<String>();
        for (User_Stats__C dist : [SELECT District__c FROM User_Stats__c where Region__c =: regnInt and District__c != null and Region__c != null and Region__c != 0 limit 10])
        {
            if(!distSet.contains(dist.District__c)) ls.add(new selectoption(dist.District__c, dist.District__c));
            distSet.add(dist.District__c);
        }
        return ls;
    }
}
Related Topic