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
vishal yadav 86vishal yadav 86 

I want to display related contacts in another page on click of account record.

VF RAMEWORK ==>
<apex:page showheader="false" tabstyle="Opportunity" controller="Acnt_Rltd_CntctClass1">
<apex:sectionHeader title="RECORDS DISPLY" subtitle="ACCOUNTS DISPLAY"/>
<apex:form >
<apex:pageblock title="MAIN BLOCK">
<apex:PageBlockTable value="{!acclist}" var="a">
<apex:column headerValue="ACCOUNT NAME">
<apex:commandlink >
{!a.name}
</apex:commandlink>
</apex:column>
<apex:column headerValue="PHONE">
{!a.phone}
</apex:column>
<apex:column headerValue="RATING">
{!a.rating}
</apex:column>
</apex:PageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>
VF CONTROLLER==>
public class Acnt_Rltd_CntctClass1 
{
    public List<Account> acclist{set;get;}
    public Acnt_Rltd_CntctClass1()
    {
      acclist = new List<Account>();
      acclist = [select name, phone, rating from account];
    }
}
Best Answer chosen by vishal yadav 86
Khan AnasKhan Anas (Salesforce Developers) 
Hi Vishal,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Controller: AccConC
public class AccConC {

    public string recid{get;set;}    
    public list<Account> Acclst{get;set;}
    public List<Contact> contacts {get; set;}
    
    public AccConDiffPageC(){
        Acclst = [SELECT Id, Name FROM Account LIMIT 10];
        contacts=null;
    }
    
    public PageReference  setupContacts() {
        contacts=[SELECT id, FirstName, LastName, Email FROM Contact WHERE AccountId=:recId];
        Pagereference pg = new Pagereference('/apex/AccConPage2');
        pg.setRedirect(false);
        return pg;
    }
}

Visualforce Page 1: AccConPage1
<apex:page controller="AccConC" sidebar="false">
    <apex:form >
        <apex:pageBlock title="AccountTable">
            <apex:pageBlockTable value="{!Acclst}" var="A">
                <apex:column headerValue="NAME OF THE ACCOUNT" > 
                    <apex:commandLink value="{!A.Name}" action="{!setupContacts}" target="_blank">
                        <apex:param value="{!A.Id}" name="idForConts" assignTo="{!recid}"/>
                    </apex:commandLink>
                </apex:column>  
                <apex:column value="{!A.Id}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Visualforce Page 2: AccConPage2
<apex:page controller="AccConC" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Contacts">
            <apex:pageBlockTable value="{!contacts}" var="contact" id="conttable">
                <apex:column value="{!contact.FirstName}"/>
                <apex:column value="{!contact.LastName}"/>
                <apex:column value="{!contact.Email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas​​​​​​​

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Vishal,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Controller: AccConC
public class AccConC {

    public string recid{get;set;}    
    public list<Account> Acclst{get;set;}
    public List<Contact> contacts {get; set;}
    
    public AccConDiffPageC(){
        Acclst = [SELECT Id, Name FROM Account LIMIT 10];
        contacts=null;
    }
    
    public PageReference  setupContacts() {
        contacts=[SELECT id, FirstName, LastName, Email FROM Contact WHERE AccountId=:recId];
        Pagereference pg = new Pagereference('/apex/AccConPage2');
        pg.setRedirect(false);
        return pg;
    }
}

Visualforce Page 1: AccConPage1
<apex:page controller="AccConC" sidebar="false">
    <apex:form >
        <apex:pageBlock title="AccountTable">
            <apex:pageBlockTable value="{!Acclst}" var="A">
                <apex:column headerValue="NAME OF THE ACCOUNT" > 
                    <apex:commandLink value="{!A.Name}" action="{!setupContacts}" target="_blank">
                        <apex:param value="{!A.Id}" name="idForConts" assignTo="{!recid}"/>
                    </apex:commandLink>
                </apex:column>  
                <apex:column value="{!A.Id}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Visualforce Page 2: AccConPage2
<apex:page controller="AccConC" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Contacts">
            <apex:pageBlockTable value="{!contacts}" var="contact" id="conttable">
                <apex:column value="{!contact.FirstName}"/>
                <apex:column value="{!contact.LastName}"/>
                <apex:column value="{!contact.Email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas​​​​​​​
This was selected as the best answer
vishal yadav 86vishal yadav 86
Thanks!!!
​​​​​​khan Anas