Populate Contacts in picklist based on selected . If i select a picklist of Accountname--> all names will be displayed. Then if i select a name "conatcts" of that names must be displayed in a new picklist .

public with sharing class AccountController1 {
public String selectedAccId{get;set;}

       public List<SelectOption> getAccountNames() {
              List<SelectOption> accOptions= new List<SelectOption>();
              accOptions.add( new SelectOption('','--Select--'));
              for( Account acc : [select Id,name from Account ] ) {
                      accOptions.add( new SelectOption(acc.Id,acc.name)); /*SelectOption list takes two parameters one is value and other one is label .In this case account name as a label and Id is the value .*/
             return accOptions;

<apex:page controller="AccountController1">
          <apex:form >
                   <apex:pageBlock title="Account Name">
                           Account Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                               <apex:selectList value="{!selectedAccId}" size="1"> 
                                           <apex:selectOptions value="{!AccountNames}" />

Hi i am able to display the records of account name picklist, But if i select a picklist name its related contacts records must be displayed in a new picklist

  • Before you go further on trying to implement this, please consider that trying to put every Account name in a picklist is definitely going to break when there are more than 50,000 Accounts and is going to be pretty unwieldy when there are more than a few hundred Accounts. Is this code only meant to run in an org that has a small number of Accounts (and Contacts)?
    – Keith C
    Feb 25, 2014 at 10:28
  • It is for a small org only please help me out Feb 25, 2014 at 10:29

2 Answers 2


You need to nest this:

<apex:actionSupport event="onchange" rerender="contactsPageBlock"/>

inside your Account apex:selectList and give the containing element of a Contacts select list the id "contactsPageBlock". The Apex that provides the apex:selectOptions of the Contacts select list must always use the current value of selectedAccId.

So when the Account select list changes the Contacts part of the page is rerendered using Contacts based on the currently selected Account id.

  • Hi thanks for the reply. I had used rerender in vf and apex but i am not getting 2nd picklist with related object records Feb 25, 2014 at 11:31
  • ... then you will have to make use of Apex's debug logging and your browsers Developer Tools to figure out where the problem is. Good luck.
    – Keith C
    Feb 25, 2014 at 12:19

Thank you for asking a question, but we're trying to avoid questions like "please write my code for me." Take a stab at the situation yourself, come back, post your code, and let's see if we can help you out. Good luck!


Thanks for posting your code. You should find the following code samples useful:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_actionRegion.htm * This shows how to use actionRegion and rerender

http://wiki.developerforce.com/page/Visualforce_DynamicEditPage * This has more explanation on how to do it.

  • Hi i had added a code which i am doing. I am getting account name picklist, if i select a accountname from picklist it;s child contacts must come on another picklist. Feb 25, 2014 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.