+ Start a Discussion
basha skbasha sk 

How to filter only selected contact from Account.Contacts dropdownlist?

Hi Everyone,

Please check the below requirement once If anybody having idea please let me know.


vf page :
----------

       <apex:pageBlock title="Registration Form">                        
            Account Names
             <apex:selectList value="{!selectedAccId}" size="1">                                 
                <apex:selectOptions value="{!AccountNames}"/>// All Salesforce Accounts
                <apex:actionSupport event="onchange" reRender="a"/>
             </apex:selectList>  <br/>                       
                         
                Related Contacts
             <apex:selectList value="{!selectedConId}" size="1"  id="a">
                <apex:selectOptions value="{!ContactNames}" /> //All contacts related to Account
            </apex:selectList><br/>    
             <apex:commandButton value="Register" action="{!createUser}" reRender="msgs" style="margin-left:40%"/>
       </apex:pageBlock>
       
       step 1: Here I selected one account from the list of all accounts then automatically populating related contacts.
       
       step2 : From the list of contacts for suppose If I select a single contact how can I get that only one contact.
               
       
       Here I completed the "step1" but I'm little confusing at "step 2"  how to filter the selected contact only from
       contacts list.

              
       This is my apex class please check it once
       
       apex class:
       -------------
       
       
public with sharing class Picklist{
    public String selectedAccId{get;set;}
    public String selectedConId{get;set;}
    
    List<System.SelectOption> conOptions{get;set;}
    List<System.SelectOption> options{get;set;}
   
    public Picklist(ApexPages.StandardController controller){
       this.login1= (LightiningEd__Webinar__c)controller.getRecord();
    }

    public List<System.SelectOption> accOptions{get;set;}
    
    public List<System.SelectOption> getAccountNames(){
                  List<System.SelectOption> accOptions= new List<System.SelectOption>();
                  accOptions.add( new System.SelectOption('','--Select--'));
                  for( Account acc : [select Id,name from Account] ){
                          accOptions.add( new System.SelectOption(String.valueOf(acc.Id),acc.name));
                  }
                 return accOptions;
           }       

           public List<System.SelectOption> getContactNames(){
               
                  System.debug('Selected account id...........'+selectedAccId );
                  List<System.SelectOption> conOptions= new List<System.SelectOption>();                 
                    if(selectedAccId != null){
               
                       for(contact con :  [select Id,name,Account.Name,accountid,FirstName,LastName,email,MobilePhone,MailingCity,MailingState,MailingCountry
                                           from contact where accountid=:selectedAccId ]){
                                               
                               conOptions.add(new System.SelectOption(String.valueOf(con.Id),con.name));
                               System.debug('conOptions:::'+conOptions);System.debug('Entered Selected contact id...........'+selectedConId);                     
               
                    }
           }return conOptions;
              
      }   
                
}
       
Thanks In Advance
Basha