+ Start a Discussion
suresh dupadasuresh dupada 

How to display related contacts in Another table using Pagereference

I wrote a code for the following output:

<apex:page standardController="Account" recordSetVar="RecordsSet">
<apex:form >
    <apex:pageBlock >
        <apex:pageblocksection >
            <apex:pageblockTable value="{!RecordsSet}" var="rec" id="TableOne">
                <apex:column ><apex:facet name="header"></apex:facet>
                    <apex:commandLink value="{!rec.name}"/>

User-added image

But, Here i need when i click on any of the records then i want to display related contacts for that account in the TableTwo in the same page using PageReference or any other methods.............

I Would appriciate any king of replay..................
You have to below:
1. You need to use custom controller and have a List<Contact> variable in controller
2. Fetch List of Accounts in constructor of controller and you can fetch all Contacts as inner query in Accout list like below:
Map<Id, Account> mAccounts = new Map<Id, Account>([SELECT Id, Name, 
(SELECT Id, Name, Email FROM Contacts) FROM Account LIMIT 100]);
When you click on any Account from List then you just need to reset List<Contact> variable you created with Id of Account you selected on UI. So you will use <apex:param> inside <apex:commandlink> tag which will set a Id variable in controller in controller and then method is called which re-renders the List of Contact variable.

You can also use it like below:
<apex:repeat value"{!mAccounts}" var="acc">
	<apex:commandlink value="{!mAccounts[acc].Name}" id="theCommandLink">
		<apex:actionSupport event="onclick" action="{!refreshContacts}" rerender="contactlist">
	<apex:commandlink> <br/>

Create a Page block with id "contactlist" which has table iterating over "lContacts" and will refresh based on click of commandlink.

PS: if this answers your question then hit Like and mark it as solution!
U JayU Jay