0

code here

<apex:page controller="AccountContactController">
    <apex:form >
        <apex:pageBlock title="Account Name">
            <apex:selectList value="{!selectedAccId}" size="1">
            <apex:selectOptions value="{!AccountNames}" />
            </apex:selectList><br/><br/>
            <b>Related Contacts</b>
            <apex:pageblockTable title="Contacts" value="{!SelectedContacts}" var="Sc">
            <apex:column value="{!Sc.name}"/>
            <apex:column value="{!Sc.phone}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

Public with sharing class AccountContactController {
  Public Id selectedAccId{get;set;} 
       List<Contact> Conlist;      
       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<Contact> getSelectedContacts(){
          Conlist=[select name,phone from Contact where Accountid=: selectedAccId];
          return Conlist;   
       }
}
0

Made minor changes. Try the below code and see if it helps
Visualforce Page

<apex:page controller="AccountContactController">
    <apex:form >
        <apex:pageBlock title="Account Name">
            <apex:selectList value="{!selectedAccId}" size="1">
                <apex:selectOptions value="{!AccountNames}" />
                <apex:actionSupport event="onchange" action="{!showContacts}" reRender="relatedContactsBlock" />
            </apex:selectList><br/><br/>

            <b>Related Contacts</b>
            <apex:pageblockTable title="Contacts" value="{!conlist}" var="sc" id="relatedContactsBlock">
                <apex:column value="{!sc.name}"/>
                <apex:column value="{!sc.phone}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller

public with sharing class AccountContactController {

    public Id selectedAccId{get;set;} 

    public List<Contact> conlist {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 PageReference showContacts(){
        conlist=[select name,phone from Contact where Accountid=: selectedAccId];
        return null;   
    }
}
0

VF page

<apex:page controller="AccountContactController">
<apex:form >
    <apex:pageBlock title="Account Name">
        <apex:actionFunction name="change" action="{!refreshContactList}" reRender="pbt" />            
        <apex:selectList value="{!selectedAccId}" size="1" onchange="change()">
        <apex:selectOptions value="{!AccountNames}" />
        </apex:selectList><br/><br/>
        <b>Related Contacts</b>
        <apex:pageblockTable title="Contacts" value="{!Conlist}" var="Sc" id="pbt">
        <apex:column value="{!Sc.name}"/>
        <apex:column value="{!Sc.phone}"/>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:form>

Controller

Public with sharing class AccountContactController {
    Public Id selectedAccId{get;set;} 
    Public List<Contact> Conlist{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 void refreshContactList(){
        Conlist = new List<Contact>();
        Conlist = [select name,phone from Contact where Accountid=: selectedAccId];
    }
}
0
**Use this code it will definitely work**
Public with sharing class AccountContactController { 
Public Id selectedAccId{get;set;} 
List<Contact> Conlist;
Public List<SelectOption> getAccountNames() 
{   
   List<SelectOption> acclist= new List<SelectOption>();
    for(Account acc : [select Id,name from Account where id=:selectedAccId] ) { 
        acclist.add(new SelectOption(acc.id,acc.name)); 
    } 
  return acclist; 
}        
Public List<Contact> getSelectedContacts(){
  Conlist=new List<Contact>();
  Conlist=[select name,phone from Contact where Accountid=: selectedAccId]; 
  return Conlist;
} 

}

  • That code is not correct i think did you check it Sumit..There are syntax errors – Deepak Agarwal Dec 8 '16 at 12:09
  • List<SelectOption> should come not List<Account> – Deepak Agarwal Dec 8 '16 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.