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
aa143aa143 

Populate Contacts in picklist based on selected Account

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}" />
                                   </apex:selectList>
                      </apex:pageBlock>
            </apex:form>
    </apex:page>




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
Sonam_SFDCSonam_SFDC
Hi,

The value you are getting from the selectlist : selectedAccId you should use this as an input for another selectoptions tag
where you can use the following SOQL to get the Account specific Contacts in the selectoption List:
select Id,name from Contact where accountID=:selectedAccId 
                        
      
        
Ashish_SFDCAshish_SFDC
Hi , 


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.


http://salesforce.stackexchange.com/questions/28725/populate-contacts-in-picklist-based-on-selected-account


Regards,
Ashish
Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

Hi Naveen,

Try the following code, so that you will have related contacts as a picklist values when you select an Account name in the First picklist.

VF page:
<apex:page controller="AccountController1_picklistrendering">
   <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="a"/>
             </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:selectList>
       </apex:pageBlock>               
    </apex:form>
</apex:page>

Controller:
public with sharing class AccountController1_picklistrendering {
    public String selectedAccId{get;set;}
    public String selectedConId{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;
           }
    }


Hope so this helps you...!

Please mark this answer a Solution, if you found this answer as helpful.

Regards,
Kamatchi Devi R
Salesforce Certified Developer | Salesforce Certified Administrator
Nagaraju MogiliNagaraju Mogili
Hi Kamatchi,

Thanks for your Help, after using the above code i am getting the below error. please suggest me.

User-added image