Another Salesforce blog!!!

Salesforce, Apex

Cascading dropdownlist


<apex:page controller="parentchildController2">

<apex:form >

<br/>
 <apex:pageBlock id="pb" title="Cascading dropdownlist">
   ParentID Selected: {!parentValue}
   <br/>
   Child Id Selected: {!contactValue}
 <br/>
<apex:selectList value="{!parentValue}" size="1" style="width:175px;">
    <apex:selectOptions value="{!ParentOptions}" /> 
    <apex:actionSupport event="onchange" reRender="pb"   />
</apex:selectList>
<br/><br/>

 <apex:selectList value="{!contactValue}" size="1" id="contacts" style="width:175px;">
    <apex:selectOptions value="{!contactOptions}"/>
    <apex:actionSupport event="onchange" reRender="pb"   />
</apex:selectList>



</apex:pageBlock>
     
</apex:form>
</apex:page>

Controller:

public class parentchildController2
{
 public String contactValue {get;set;} 
 public List<SelectOption> getcontactOptions() {

           SelectOption[] contactOptions = new SelectOption[]{};

        contactOptions.add(new SelectOption('--select--','--select--'));
        for (Contact contact:[select Id, Name from Contact where AccountId =: parentValue ORDER BY NAME])
        {
            contactOptions.add(new SelectOption(contact.Id,contact.Name));
        } 
        countChild = contactOptions.size();
        return contactOptions;
    }


public Integer countChild {get;set;}
 
 public String parentValue {get; set;}

    // account select options for the opportunities
    public List<SelectOption> getParentOptions() {

        List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('--select--','--select--'));
        for (Account record : [select Id, Name from Account LIMIT 100])
        {
            options.add(new SelectOption(record.Id,record.Name));
        }
        
        return options;
    } 
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*