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
shan jakkulashan jakkula 

related contacts in pageblocktable for selected account

Hi,

I'm new salesforce and i have Requirement like...

I have all Account Names in Dropdown(using Apex:selectList i have done this), if select any of the Account Name from the Dropdown and after clicking on commandButton ,i need to Display all related contacts for the particular Account in pageBlockTable.Do i need to write inner Class for this? if yes please give me some sample code to achieve this.

Thanks in Advance
shan
karthikeyan perumalkarthikeyan perumal
 Hello, 

This is the code t display related contact associated with Account in  button Click 

Class
public class DiplayRelatedContacts {
   public List<Account> accList{get;set;}
   public list<Contact> conList{get;set;}
   public String accId{get;set;}
   public DiplayRelatedContacts(){
       accList=[SELECT Id,Name,AccountNumber FROM Account LIMIT 10];
   
   }
    public PageReference dispalyContact() {
       if(accId != null)
       conList=[SELECT id,FirstName,LastName FROM COntact WHERE AccountId=:accId];
        return null;
    }
}

VF page
 
<apex:page sidebar="false" controller="getrelatedcontacts" action="{!getaccounts}">
<apex:sectionHeader title="contacts of accounts"/>
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Display" action="{!getcontacts}" reRender="aj" />
</apex:pageBlockButtons>
<apex:actionRegion >
<apex:selectList size="1" multiselect="false" label="Account" value="{!selectedid}">

<apex:selectOptions value="{!listoptions}">
</apex:selectOptions>
</apex:selectList>
</apex:actionRegion>
<apex:pageBlockTable value="{!listcontact}" var="a" id="aj">
<apex:column headerValue="first name" value="{!a.firstname}"/>
<apex:column headerValue="second name" value="{!a.lastname}"/>
<apex:column headerValue="phone" value="{!a.phone}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

Hope this will help you, 
Mark Best ANSWER if its works

Thanks
karthik
 
shan jakkulashan jakkula
Thank you..Its working with slight changes....
karthikeyan perumalkarthikeyan perumal
Mark best ANSWER if it's work for you

thanks
karthik.
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner
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:pageBlock >
    <apex:pageBlock > -->
        <apex:pageBlockButtons location="Top">
            <apex:commandButton value="Display" action="{!dispalyContact}"  /><!-- reRender="aj" -->
        </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; }

     
}