function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vijay kumar kvijay kumar k 

How to create vf page to show account names in picklist, select one of it shows the corresponding contact's in picklist

Actually my scenario is 
In vf page shows picklist values are account names and onchange the page shows contact picklist values are child contacts names of above account names. Finally onchange the page shows another picklist option & value of this is opportunity names of above selected contact name. 
Shows the errors if no contact of account and no opportunity of contact when really does not have child record in parent level 
Best Answer chosen by vijay kumar k
Akshay_DhimanAkshay_Dhiman
Hi Vijay,

<----------- VF Page --------------->
<apex:page controller="AccountContactOppRelationController">
<apex:form >
<apex:pageBlock id="PageBlockSection">
<apex:pageMessages ></apex:pageMessages>

  <apex:selectList size="1" value="{!selectedAccId}">
  <apex:actionSupport event="onchange" action="{!conValNew}" reRender="ContactId , PageBlockSection , OppId">
  </apex:actionSupport>
          <apex:selectOptions value="{!AccountList}"></apex:selectOptions>
      </apex:selectList>
    
      <apex:selectList size="1" id="ContactId" value="{!selectedConAccId}" rendered="{!ContactListRender}">
      <apex:actionSupport event="onchange" action="{!OppValNew}" reRender="OppId , PageBlockSection">
  </apex:actionSupport>
          <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
      </apex:selectList>
      <apex:selectList size="1" id="OppId" rendered="{!OppListRender}">
          <apex:selectOptions value="{!Opportunitylist}"></apex:selectOptions>
      </apex:selectList>
      </apex:pageBlock>
      </apex:form>
</apex:page>

<---------- Its Associated controller ---------------->
public class AccountContactOppRelationController {
    
    public List<SelectOption> contactlist{set;get;}
    public List<SelectOption> Opportunitylist{set;get;}
    public Boolean ContactListRender{get;set;}
    public Boolean OppListRender{get;set;}
    public Id selectedAccId{get;set;} 
    public Id selectedConAccId{get;set;} 
    public AccountContactOppRelationController()
    {
        contactlist = new List<SelectOption>();
        Opportunitylist = new List<SelectOption>();
        
        
    }
    public List<SelectOption> AccountList
    {
        get
        {
            List<Account> accTemp = new List<Account>();
            accTemp = [Select Id,Name from Account];
            
            AccountList = new List<SelectOption>();
            
            for(Account acc : accTemp)
            {
                AccountList.add(new SelectOption(acc.Id, acc.Name));
            }
            return AccountList;
        }
        set;
    }
    
    public PageReference conValNew()
    {        
        contactlist.clear();
        List<Contact> ContactTemp = new List<Contact>();
        System.debug('selectedAccId--->>>' + selectedAccId);
        if(selectedAccId!=null)
        {
            ContactTemp = [Select Id,firstname,lastname , AccountId from contact where AccountId =: selectedAccId];
        }
        
        System.debug('ContactTemp---->>>>>' + ContactTemp);
        if(ContactTemp.isEmpty())
        {
            ContactListRender = false;
            Opportunitylist.clear();
            OppListRender = false;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No Contact Record Exist Regarding this Account'));
        }
        else{
            for(contact con : ContactTemp)
            {
                ContactListRender = true;
                contactlist.add(new SelectOption(con.AccountId, con.lastname));
                System.debug('contactlist--->>>>' + contactlist);
            }
            
        }
        
        
        return null;
    }
    
    public PageReference OppValNew()
    {        
        Opportunitylist.clear();
        List<Opportunity> OpportunityTemp = new List<Opportunity>();
        System.debug('selectedConAccId--->>>' + selectedConAccId);
        OpportunityTemp = [Select Id , Name from Opportunity where AccountId =: selectedConAccId];
        System.debug('OpportunityTemp---->>>>>' + OpportunityTemp);
        if(OpportunityTemp.isEmpty())
        {
            OppListRender = false;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No Opportunity Record Exist Regarding this Contact'));
        }
        else{
            OppListRender = true;
            for(Opportunity opp : OpportunityTemp)
            {
                Opportunitylist.add(new SelectOption(opp.Id, opp.Name));
                System.debug('Opportunitylist--->>>>' + Opportunitylist);
            }
        }
        
        
        return null;
    }
    public List<SelectOption> getcontactlist()
    {
        
        return contactlist;
    }
    public List<SelectOption> getOpportunitylist()
    {
        
        return Opportunitylist;
    }
    public Boolean getContactListRender()
    {
        
        return ContactListRender;
    }
     public Boolean getOppListRender()
    {
        
        return OppListRender;
    }
}


Please mark as best answer if it helps you.

Thanks 
Akshay

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Vijay,

Below is the sample code which I have tested in my org and it is working fine. 

Visualforce:
<apex:page controller="AccConPicklistC">
    <apex:form >
        <apex:pageMessages/>
        <apex:pageBlock title="Account Name">
            Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>
                <apex:actionSupport event="onchange" />
            </apex:selectList>
            
            <br/><br/>
            
            Related Contact Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:selectList value="{!selectedConId}" size="1"  id="a">
                <apex:selectOptions value="{!ContactNames}" />
                <apex:actionSupport event="onchange" />
            </apex:selectList>
            
            <br/><br/>
            
            Related Opportunity Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:selectList value="{!selectedOppId}" size="1"  id="o">
                <apex:selectOptions value="{!OpportunityNames}" />
            </apex:selectList>
        </apex:pageBlock>               
    </apex:form>
</apex:page>

Controller:
public class AccConPicklistC {
    
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
    public String selectedOppId{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));
        }
        return accOptions;
    }
    
    public List<SelectOption> getContactNames() {
        System.debug('Entered ContactNames account id...........'+selectedAccId );
        List<SelectOption> conOptions= new List<SelectOption>();
        List<SelectOption> options = new List<SelectOption>();
        if(selectedAccId != null)
        {     
            for( contact con : [select Id,name,accountid from contact where accountid=:selectedAccId ] ) {
                conOptions.add( new SelectOption(con.Id,con.name));
            }
        }                  
        else
        {
            conOptions.add( new SelectOption('--None--','--None--'));
        }
        return conOptions;
    }
    
    public List<SelectOption> getOpportunityNames() {
        List<SelectOption> oppOptions= new List<SelectOption>();
        List<SelectOption> options = new List<SelectOption>();
        if(selectedConId != null)
        {     
            for( Opportunity con : [select Id, Name, accountid from Opportunity 
                                    where accountid IN (SELECT AccountId from Contact WHERE Id =:selectedConId)]) {
                oppOptions.add( new SelectOption(con.Id,con.name));
            }
            System.debug('oppOptions--> ' + oppOptions);
        }                  
        else
        {
            oppOptions.add( new SelectOption('--None--','--None--'));
        }
        return oppOptions;
    }
}

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi vijay,
You can try the following code. Hope this will help you

Vf Page:
 
<apex:page controller="AccContactController">
    <apex:form >
        <apex:pageBlock title="Account Name">
            Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>
                <apex:actionSupport event="onchange" reRender="Cont,Oppor"/>
            </apex:selectList>
            
            <br/><br/>
                Contact Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:selectList value="{!selectedConId}" size="1"  id="Cont">
                    <apex:selectOptions value="{!ContactNames}" />
                    <apex:actionSupport event="onchange"/>
                </apex:selectList>       
                <br/><br/>
                
                Opportunity Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:selectList value="{!selectedOppId}" size="1"  id="Oppor">
                    <apex:selectOptions value="{!OpportunityNames}" />
                </apex:selectList>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class AccContactController {
    public Id selectedAccId{get;set;} 
    public Id selectedConId{get;set;}
    public Id selectedOppId{get;set;} 
      
    public List<Contact> conlist {get;set;} 
    public List<Opportunity> opplist {get;set;}
    
    public List<SelectOption> getAccountNames() {
        List<SelectOption> accOptions= new List<SelectOption>();
        system.debug(selectedAccId);
        accOptions.add( new SelectOption('','--Select--'));
        for(Account acc : [select Id,name from Account ] ) {
            accOptions.add(new SelectOption(acc.Id,acc.name));
        }
        return accOptions;
    }
    
    public List<SelectOption> getContactNames() {
        System.debug('Entered ContactNames account id...........'+selectedAccId );
        List<SelectOption> conOptions= new List<SelectOption>();
        List<SelectOption> options = new List<SelectOption>();
        if(selectedAccId != null)
        {     
            for( contact con : [select Id,name,accountid from contact where accountid=:selectedAccId ] ) {
                conOptions.add( new SelectOption(con.Id,con.name));
            }
        }                  
        else
        {
            conOptions.add( new SelectOption('--None--','--None--'));
        }
        return conOptions;
    }
    
    public List<SelectOption> getOpportunityNames()
    {
        List<SelectOption> oppOptions= new List<SelectOption>();
        List<SelectOption> options = new List<SelectOption>();
        if(selectedAccId != null)
        {  
            for(Opportunity opp :[Select Id, Name,AccountId FROM Opportunity WHERE AccountId =:selectedAccId])
            {
                system.debug('opportunity Obj:' + opp);
                oppOptions.add( new SelectOption(opp.Id,opp.name));
            }
        }
        else
        {
            system.debug('opportunity else');
            oppOptions.add( new SelectOption('--None--','--None--'));
        }
        return oppOptions;
    }
}


Thank you
Ajay Dubedi
 
vijay kumar kvijay kumar k
thank you this will working.But i want all 3 picklists in one by one.i.e first select account then only display picklist label of contacts,if there is no contact doesn't display the picklist lable also just disply error message"there is contact of this account".
Similarly in opportunity.
please help me to do this.
thanky you
shyamesh k rshyamesh k r
Hi vijay,
Above you have metioned to change the opportunity dropdown on selected contact . But is it to have a custom relationship with contact because there is no standard relationship.If you meant to change Opportunity dropdown according to Account ,here is the code:

visualforce page:
<apex:page controller="ContactAndOpportunityOnAccount">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:OutputPanel >
                    <apex:selectList value="{!AccountId}" size="1" multiselect="false">
                        <apex:selectOptions value="{!AccountList}" />
                        <apex:actionSupport event="onchange" reRender="ConDetails" action="{!getContactList}"/>
                    </apex:selectList>
                </apex:OutputPanel>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:OutputPanel >
                    <apex:selectList id="ConDetails" value="{!ContactId}" size="1" multiselect="false" readonly="true">   
                        <apex:selectOptions value="{!ListOfContact}"/>
                        <apex:actionSupport event="onchange" reRender="OppDetails" action="{!getOpportunityList}"/>
                    </apex:selectList>
                </apex:OutputPanel>
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:OutputPanel >
                    <apex:selectList id="OppDetails" value="{!OpportunityId}" size="1" multiselect="false" readonly="true">   
                        <apex:selectOptions value="{!ListOfOpportunity}"/>                      
                    </apex:selectList>
                </apex:OutputPanel>
            </apex:pageBlockSection>            
        </apex:pageBlock>
    </apex:form>
</apex:page>

apex controller:
public class ContactAndOpportunityOnAccount {    
    public string AccountId{get;set;}
    public string ContactId{get;set;}
    public string OpportunityId{get;set;}
    List<SelectOption> ListOfContact {get;set;}
    List<SelectOption> ListOfOpportunity {get;set;}
    
    public ContactAndOpportunityOnAccount(){
        AccountId='';
        ContactId='';
        OpportunityId='';
        ListOfContact =new List<SelectOption>();
        ListOfOpportunity=new List<SelectOption>();
    }
    
    public static List<SelectOption> getAccountList(){
        List<Account> AccountList = [select id,Name from Account Limit 1000];
        List<SelectOption> ListOfAccount =new List<SelectOption>();
        ListOfAccount.add(new SelectOption( ' ' ,'---Select---'));
        
        for(Account a:AccountList){
            ListOfAccount.add(new SelectOption(a.Id,a.Name));
        }
        return ListOfAccount;
    }
    
    public void getContactList(){
        system.debug('aasdas'+AccountId);
        List<Contact> ContactList = [select id,name from Contact where account.Id =: AccountId];
        //List<SelectOption> ListOfContact =new List<SelectOption>();
        ListOfContact.add(new SelectOption( ' ' ,'---Select---'));
        
        for(Contact con:ContactList){
            ListOfContact.add(new SelectOption(con.Id,con.Name));
        }
        system.debug('listcon'+ListOfContact);       
        
    }
    
    public void getOpportunityList(){
        List<Opportunity> OpportunityList = [select id,name from Opportunity where account.Id =: AccountId];
        //List<SelectOption> ListOfOpportunity =new List<SelectOption>();
        ListOfOpportunity.add(new SelectOption( ' ' ,'---Select---'));
        
        for(Opportunity opp:OpportunityList){
            ListOfOpportunity.add(new SelectOption(opp.Id,opp.Name));
        }
        system.debug('listopp'+ListOfOpportunity);
    }
    
    public List<SelectOption> getListOfContact() {
        return ListOfContact;
    } 
    
    public List<SelectOption> getListOfOpportunity() {
        return ListOfOpportunity;
    } 
    
}

vijay kumar kvijay kumar k
thank you sir,
that is my fault but how to show error message if there is no contacts of selected account and if  have a contact leave error .




thanky you
Akshay_DhimanAkshay_Dhiman
Hi Vijay,

<----------- VF Page --------------->
<apex:page controller="AccountContactOppRelationController">
<apex:form >
<apex:pageBlock id="PageBlockSection">
<apex:pageMessages ></apex:pageMessages>

  <apex:selectList size="1" value="{!selectedAccId}">
  <apex:actionSupport event="onchange" action="{!conValNew}" reRender="ContactId , PageBlockSection , OppId">
  </apex:actionSupport>
          <apex:selectOptions value="{!AccountList}"></apex:selectOptions>
      </apex:selectList>
    
      <apex:selectList size="1" id="ContactId" value="{!selectedConAccId}" rendered="{!ContactListRender}">
      <apex:actionSupport event="onchange" action="{!OppValNew}" reRender="OppId , PageBlockSection">
  </apex:actionSupport>
          <apex:selectOptions value="{!contactlist}"></apex:selectOptions>
      </apex:selectList>
      <apex:selectList size="1" id="OppId" rendered="{!OppListRender}">
          <apex:selectOptions value="{!Opportunitylist}"></apex:selectOptions>
      </apex:selectList>
      </apex:pageBlock>
      </apex:form>
</apex:page>

<---------- Its Associated controller ---------------->
public class AccountContactOppRelationController {
    
    public List<SelectOption> contactlist{set;get;}
    public List<SelectOption> Opportunitylist{set;get;}
    public Boolean ContactListRender{get;set;}
    public Boolean OppListRender{get;set;}
    public Id selectedAccId{get;set;} 
    public Id selectedConAccId{get;set;} 
    public AccountContactOppRelationController()
    {
        contactlist = new List<SelectOption>();
        Opportunitylist = new List<SelectOption>();
        
        
    }
    public List<SelectOption> AccountList
    {
        get
        {
            List<Account> accTemp = new List<Account>();
            accTemp = [Select Id,Name from Account];
            
            AccountList = new List<SelectOption>();
            
            for(Account acc : accTemp)
            {
                AccountList.add(new SelectOption(acc.Id, acc.Name));
            }
            return AccountList;
        }
        set;
    }
    
    public PageReference conValNew()
    {        
        contactlist.clear();
        List<Contact> ContactTemp = new List<Contact>();
        System.debug('selectedAccId--->>>' + selectedAccId);
        if(selectedAccId!=null)
        {
            ContactTemp = [Select Id,firstname,lastname , AccountId from contact where AccountId =: selectedAccId];
        }
        
        System.debug('ContactTemp---->>>>>' + ContactTemp);
        if(ContactTemp.isEmpty())
        {
            ContactListRender = false;
            Opportunitylist.clear();
            OppListRender = false;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No Contact Record Exist Regarding this Account'));
        }
        else{
            for(contact con : ContactTemp)
            {
                ContactListRender = true;
                contactlist.add(new SelectOption(con.AccountId, con.lastname));
                System.debug('contactlist--->>>>' + contactlist);
            }
            
        }
        
        
        return null;
    }
    
    public PageReference OppValNew()
    {        
        Opportunitylist.clear();
        List<Opportunity> OpportunityTemp = new List<Opportunity>();
        System.debug('selectedConAccId--->>>' + selectedConAccId);
        OpportunityTemp = [Select Id , Name from Opportunity where AccountId =: selectedConAccId];
        System.debug('OpportunityTemp---->>>>>' + OpportunityTemp);
        if(OpportunityTemp.isEmpty())
        {
            OppListRender = false;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,'No Opportunity Record Exist Regarding this Contact'));
        }
        else{
            OppListRender = true;
            for(Opportunity opp : OpportunityTemp)
            {
                Opportunitylist.add(new SelectOption(opp.Id, opp.Name));
                System.debug('Opportunitylist--->>>>' + Opportunitylist);
            }
        }
        
        
        return null;
    }
    public List<SelectOption> getcontactlist()
    {
        
        return contactlist;
    }
    public List<SelectOption> getOpportunitylist()
    {
        
        return Opportunitylist;
    }
    public Boolean getContactListRender()
    {
        
        return ContactListRender;
    }
     public Boolean getOppListRender()
    {
        
        return OppListRender;
    }
}


Please mark as best answer if it helps you.

Thanks 
Akshay
This was selected as the best answer
vijay kumar kvijay kumar k
hi sir, 
how can i over ride a clone button on  above vf page and shows the how many no of click the clone button used in custom field at account object.