0

Following is my code. Countries are displayed in the picklist but not states. Pls help.

Apex Page:

<apex:page controller="SampleController">
<apex:form >
<apex:pageblock >
<apex:pageBlockSection >
<apex:outputLabel value="Country" />
<apex:actionRegion >
<apex:selectList multiselect="false" value="{!selectOption}" size="1">
<apex:selectOptions value="{!options}"></apex:selectOptions>
<apex:actionSupport event="onchange" rerender="selectState" />
</apex:selectList>
</apex:actionRegion>
</apex:pageBlockSection>


<apex:pageBlockSection >
<apex:outputLabel value="State" />
<apex:actionRegion >
<apex:selectList multiselect="false" value="{!selectState}" size="1"    id="selectState">
<apex:selectOptions value="{!states}"></apex:selectOptions>
</apex:selectList>
</apex:actionRegion>
</apex:pageBlockSection>


</apex:pageblock>
</apex:form>

</apex:page>

Controller:

    public without sharing class SampleController
{

    public SampleController()
    {
    }

    public String selectOption;

    public SelectOption[] getOptions() {
    SelectOption[] options = new SelectOption[] {};
    options.add(new SelectOption( ' ' ,'---Select---'));
    for (AggregateResult ar : [
            Select billingcountry n from account where billingcountry!=null group by billingcountry
            ]) {
        String n = (String) ar.get('n');
        options.add(new SelectOption(n, n));
    }
    return options;
}

    public void setselectOption(String selectOption)
    {
    this.selectOption=selectOption;
    }

    public string getselectOption()
    {
    return selectOption;
    }

    public String selectState {get; set;}

    public SelectOption[] getstates() {
    SelectOption[] stateoptions = new SelectOption[] {};
    stateoptions.add(new SelectOption( ' ' ,'---Select---'));
    for (AggregateResult arstate : [
            Select billingstate s from account where  billingcountry='" + selectOption + "' and billingstate!=null group by billingstate
            ]) {
        String s = (String) arstate.get('s');
        stateoptions.add(new SelectOption(s, s));
    }
    return stateoptions;
   }
   }
3
  • i dont see any fault in the code. can you check if query is returning any results..? May 4, 2016 at 10:45
  • Yes, Query is fetching the values. Checked it in query editor May 4, 2016 at 10:48
  • ok so rerender should be id of page block.. this might be problem. May 4, 2016 at 10:49

1 Answer 1

3

This value:

rerender="selectState"

should be the ID of a page element of a surrounding block not just the field you want to rerender:

<apex:pageblock id="pb">
...
<apex:actionSupport event="onchange" rerender="pb" />
2
  • I have added id="selectState" to pageblocksection but still its not working. I have checked the query also by hard coding the value of a country and I am able to see the states in the dropdown. So I think the value of country is not passed into the query. Is there any error in the way I have accessed the value of the country dropdown? May 5, 2016 at 8:28
  • @NagashruthiChakilam The code you have posted looks invalid so its hard to comment accurately: instead of billingcountry='" + selectOption + "' I would have expected billingcountry=:selectOption. Suggest you edit the question to include the exact code you are using.
    – Keith C
    May 5, 2016 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .