1

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;   
       }
}

4 Answers 4

1

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];
    }
}
4
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;
} 

}

2
  • That code is not correct i think did you check it Sumit..There are syntax errors Commented Dec 8, 2016 at 12:09
  • List<SelectOption> should come not List<Account> Commented Dec 8, 2016 at 12:11
-1

Use Below Code: Showing child records in Pageblock Table after selection of a Parent record from picklist.

VF Page: <apex:page sidebar="false" controller="DiplayRelatedContacts"> <apex:sectionHeader title="contacts of accounts"/> apex:form <apex:pageBlock > <apex:pageBlockSection title="Select the Account/Parent Name" > <apex:actionRegion > <apex:selectList value="{!selectParentRecID}" size="1" multiselect="false" > <apex:actionSupport event="onchange" action="{!dispalyContact}" reRender="aj" /> <apex:selectOptions value="{!ListOfAccounts}"/> </apex:selectList> </apex:actionRegion> </apex:pageBlockSection> --> <apex:pageBlockButtons location="Top"> <apex:commandButton value="Display" action="{!dispalyContact}" /> </apex:pageBlockButtons>

    <apex:pageBlockTable value="{!conList}" var="a" id="aj">
        <apex:column headerValue="Child Name" value="{!a.Name}"/>
        <apex:column headerValue="City" value="{!a.selflearning__city__c}"/>
        <apex:column headerValue="Parent Name" value="{!a.selflearning__Parent_Object__r.name}"/>
         
    </apex:pageBlockTable>
</apex:pageBlock>

</apex:form>

</apex:page>

Class: public class DiplayRelatedContacts {

public List<selflearning__Pobject__c> accList{get;set;}
public list<selflearning__Cobject__c> conList{get;set;}

public DiplayRelatedContacts()
{
   //accList=[SELECT Id,Name FROM selflearning__Pobject__c LIMIT 10];
}
public PageReference dispalyContact() {
   if(selectParentRecID != null)
   conList=[SELECT id,name,selflearning__Parent_Object__r.name,selflearning__city__c FROM selflearning__Cobject__c WHERE selflearning__parent_object__c=:selectParentRecID];
    return null;
}



  public List<SelectOption> getListOfAccounts()
{
   List<selflearning__Pobject__c> AccountList = [SELECT Id,Name FROM selflearning__Pobject__c] ;
   System.debug('selflearning__Pobject__c: '+AccountList.size());
   List<SelectOption> AccountOptionList = new List<SelectOption>();
   AccountOptionList .add(new SelectOption( ' ' ,'---Select---'));
   for(selflearning__Pobject__c acc : AccountList )
   {
        AccountOptionList.add(new SelectOption(acc.ID , acc.Name));
   }
        return AccountOptionList ;
}  

public String selectParentRecID { get; set; }

 

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .