+ Start a Discussion

custom account and contact picklist



  Need a visual force page .


 Having all account list as picklist , based on selected account a contact picklist(contacts related to particular account) should be shown ..Whenever a contact is selected a text field contains a phone number of the contact will be dispalyed.


Can any one please help in developing this.


Thanks in advance.

Best Answer chosen by NaveenReddy


Please find the below code for your requirement:


Apex  code:


public class ContactPicklistCont {

    Public String selectedAcc{get;set;}
    Public String selectedCon{get;set;}
    public List<SelectOption> conList{get;set;}
    public String phoneNumber{get;set;}

    public List<SelectOption> getAccounts() {
         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));
         return accOptions;
   public void contactList() {
       conList = new List<SelectOption>();
       conList.add( new SelectOption('','--Select--'));
       if( selectedAcc != null && selectedAcc.length() > 0 ) {
           for( Contact con : [select Id,name from Contact where AccountId =:selectedAcc]) {
               conList.add( new SelectOption(con.Id,con.name));
   public void findContactphone() {
        if( selectedCon != null && selectedCon.length() > 0 ) {
            Contact con = [select Id,phone from Contact where id =:selectedCon];
            phoneNumber = con.Phone;


Visual force page:


<apex:page controller="ContactPicklistCont">
        <apex:pageBlock title="Account Detail">
                    <apex:selectList value="{!selectedAcc}" size="1">
                        <apex:selectOptions value="{!Accounts}"></apex:selectOptions>
                        <apex:actionSupport action="{!contactList}" event="onchange"/>
                    <apex:selectList value="{!selectedCon}" size="1">
                        <apex:selectOptions value="{!conList}"></apex:selectOptions>
                        <apex:actionSupport action="{!findContactphone}" event="onchange"/>
                <apex:pageBlockSectionItem rendered="{!IF(phoneNumber != null,true,false)}">
                    <apex:outputText value="Phone Number"></apex:outputText>
                    <apex:outputText value="{!phoneNumber}"></apex:outputText>



1. To create custom picklist we need to use "apex:selectOptions" in visual force.this component not have event attribute so we use "apex:actionSupport " to call apex method.



Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.