+ Start a Discussion
basha skbasha sk 

Contact picklist doesn't updated based on particular account selection?

Hi All,

   I'm trying to display all contacts related to a particular account.

  for that i have developed below apex class.It displays all salesforce accounts but if i select any account related contacts doesn't display please check my visualforce page and apex class.


VF Page :
-------------

apex:page standardController="Webinar_Attendees_Status__c" extensions="Picklist" action="{!getAccountsWithContacts}">
<apex:form >
<apex:pageBlock title="Registration Form">
      Account Names&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <apex:selectList value="{!selectedAccId}" size="1">
   <apex:selectOptions value="{!accOptions}"/>
<apex:actionSupport event="onchange" reRender="a"/>
</apex:selectList>
<br/><br/> Related Contacts&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<apex:actionFunction name="DoFilter" action="{!ContactsByAccountId}" rerender="out" status="loading">
<apex:param assignTo="{!pAccountId}" value="" name="letterowner"/>
</apex:actionFunction> 

<apex:pageBlockSection >
<apex:commandButton value="Register" action="{!populateContactDetails}" reRender="msgs" style="margin-left:50%"/>
<apex:pageMessages id="msgs"/> </center> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>

Apex class:
----------------

public with sharing class Picklist{

    public Map<Id, Account> accountIdRecordMap {get; set;}
    public LightiningEd__Webinar_Attendees_Status__c login1 {get; set;}
    
    public List<System.SelectOption> accOptions{get;set;}
    public Id selectedAccId {get; set;}
    public Id pAccountId{get;set;}
    public List<System.SelectOption> conOptions{get;set;}
    public Id selectedContactId {get; set;}
    
    public Contact selectedContact {get; set;}
    public Lead customObjInstance {get; set;}
      //Constructor
    public Picklist(ApexPages.StandardController controller){
        this.login1 = (LightiningEd__Webinar_Attendees_Status__c)controller.getRecord();
        init();
    } 

    //Retrieve most recent 500 Accounts and associated Contacts
    public void getAccountsWithContacts(){
    
        if(this.accountIdRecordMap == null){
            
            this.accountIdRecordMap = new Map<Id, Account>([Select Id, Name,(Select Id,name,accountid,LastName,email,MobilePhone,MailingCity,MailingState,MailingCountry From Account.Contacts )From Account]);
        }
                this.getAccountNames();
                //this.getContactsByAccountId();
    }
    
    //Populate account picklist options
    public void getAccountNames(){
        
        if(this.accOptions.size() == 0){
        
            this.accOptions.add(new System.SelectOption('' , '--Select--'));
        
            for(Id accRecId :accountIdRecordMap.keySet()){
                this.accOptions.add(
                    new System.SelectOption(
                        accRecId, 
                        accountIdRecordMap.get(accRecId).Name
                    )
                );
            }//for
        } 
    }
    
    //Populate related Contact picklist options
    public void getContactsByAccountId(Id pAccountId){

        this.conOptions.add(new System.SelectOption('' , '--Select--'));
        if(accountIdRecordMap.containsKey(pAccountId)){

            for(Contact contactRec :((Account)accountIdRecordMap.get(pAccountId)).Contacts){
            
                this.conOptions.add(
                    new System.SelectOption(
                        contactRec.Id, 
                        contactRec.Name
                    )
                );
            }//for
        }
    }
    
    //Retrieves the selectedContact Record information based on the selected Account Id
    public void getSelectedContact(){
    
        if(accountIdRecordMap.containsKey(pAccountId)){

            for(Contact contactRec :((Account)accountIdRecordMap.get(pAccountId)).Contacts){
                if(contactRec.Id == selectedContactId){
                    this.selectedContact = contactRec;
                }
            }
        }//if
    }
    
    //Assumes selectedContact is populated with the Contact record information
    public void populateContactDetails(){
    
        if(this.selectedContact <> null){
    
                firstname = this.selectedContact.FirstName;
                lastname = this.selectedContact.LastName;
                email = this.selectedContact.Email;
                mobileno = this.selectedContact.MobilePhone;
                mailingcity = this.selectedContact.MailingCity;
               LeadExportToAdobe.exportLead(firstname,lastname,email,login1.LightiningEd__Register_Password__c,login1.LightiningEd__Conform_Password__c,company,mailingcity,mailingstate,mailingcountry,mobileno); 
  
            
        }
    }
    
    //Private Methods
    private void init(){
        accOptions = new  List<System.SelectOption>();
        conOptions = new  List<System.SelectOption>();
    }
}



Thanks,