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
User One 35User One 35 

How to display all child records in one place (Parent Object)

Display all Child Records on Parent Object
I want to display all child records on parent object  here is my code 
But it displaying only contact records, I want to display all records which related to the parent object (it means all child objects records should display in parent Account object) please help me how to use triggers here.

Apex Code:
public class ContactsRecordsToAccounts{
  
    Public Id accID;
    public List<Contact> contactList{get;set;}
    public ContactsRecordsToAccounts(){
   contactList = new List<Contact>();
   accID=  ApexPages.currentPage().getParameters().get('acId');
   contactList =  [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];
 }
}

Visualforce Page Code:

<apex:page controller="ContactsRecordsToAccounts" sidebar="false" showHeader="false">
     <apex:pageBlock >
           <apex:pageBlockTable value="{!contactList}" var="con">
                      <apex:column value="{!con.FirstName}"/>
                       <apex:column value="{!con.LastName}"/>
                       <apex:column value="{!con.Phone}"/>
                       <apex:column value="{!con.Email}"/>
           </apex:pageBlockTable>
        </apex:pageBlock>
</apex:page>
Best Answer chosen by User One 35
VishnuYVishnuY
Hello,

You have to query all the objects in the same way you did for contacts. And display them as did for Contacts. See my example code after adding opps.

Class:
public class ContactsRecordsToAccounts{
  
    Public Id accID;
    public List<Contact> contactList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public ContactsRecordsToAccounts(){
   contactList = new List<Contact>();
   accID=  ApexPages.currentPage().getParameters().get('acId');
   contactList =  [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];
oppList=  [SELECT Name,Company FROM Oppotunity WHERE AccountID = : accID];
 }
}
Visualforce Page:
<apex:page controller="ContactsRecordsToAccounts" sidebar="false" showHeader="false">
     <apex:pageBlock  title="Contacts">
           <apex:pageBlockTable value="{!contactList}" var="con">
                      <apex:column value="{!con.FirstName}"/>
                       <apex:column value="{!con.LastName}"/>
                       <apex:column value="{!con.Phone}"/>
                       <apex:column value="{!con.Email}"/>
           </apex:pageBlockTable>
        </apex:pageBlock>

 <apex:pageBlock  title="Opportunities">
           <apex:pageBlockTable value="{!opptList}" var="con">
                      <apex:column value="{!con.Name}"/>
                       <apex:column value="{!con.Company}"/>
           </apex:pageBlockTable>
        </apex:pageBlock>
</apex:page>


Add all the fields you want to the query and also in Visualforce.
 

All Answers

VishnuYVishnuY
Hello,

You have to query all the objects in the same way you did for contacts. And display them as did for Contacts. See my example code after adding opps.

Class:
public class ContactsRecordsToAccounts{
  
    Public Id accID;
    public List<Contact> contactList{get;set;}
    public List<Opportunity> oppList{get;set;}
    public ContactsRecordsToAccounts(){
   contactList = new List<Contact>();
   accID=  ApexPages.currentPage().getParameters().get('acId');
   contactList =  [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];
oppList=  [SELECT Name,Company FROM Oppotunity WHERE AccountID = : accID];
 }
}
Visualforce Page:
<apex:page controller="ContactsRecordsToAccounts" sidebar="false" showHeader="false">
     <apex:pageBlock  title="Contacts">
           <apex:pageBlockTable value="{!contactList}" var="con">
                      <apex:column value="{!con.FirstName}"/>
                       <apex:column value="{!con.LastName}"/>
                       <apex:column value="{!con.Phone}"/>
                       <apex:column value="{!con.Email}"/>
           </apex:pageBlockTable>
        </apex:pageBlock>

 <apex:pageBlock  title="Opportunities">
           <apex:pageBlockTable value="{!opptList}" var="con">
                      <apex:column value="{!con.Name}"/>
                       <apex:column value="{!con.Company}"/>
           </apex:pageBlockTable>
        </apex:pageBlock>
</apex:page>


Add all the fields you want to the query and also in Visualforce.
 
This was selected as the best answer
User One 35User One 35
Hi Vishnu,

Thanks for reply but I want to do with trigger please can you help me how to do with triggers 

Thank  you 
VishnuYVishnuY
You cannot link visualforce to a trigger. Its has to be Controller class. 

If you dont want to use a controller you can directly display related lists in VF page. See example below.
<apex:page sidebar="false" showHeader="false">
      <apex:relatedList list="Opportunities" />
       <apex:relatedList list="Contacts" />
</apex:page>
Please mark my response as correct answer if this helped you.
User One 35User One 35
Thank you vishnu is there any possibility to do with triggers if there can you please help me My task is to do with triggers  
VishnuYVishnuY
Is there any specific reason you want to use triggers. Whats your task? To display all the Child records right?
User One 35User One 35
Interview they ask me to do display all child records (not one child object all child objects records  for example Account is parent object and child objects are Case, Contact, Opportunity all these child records should display in parent account object  ) using triggers but I tried VF and Apex but I don't know how to do with triggers 
VishnuYVishnuY
I believe, something is missing in the requirement. There is no way you can use Visualforce and triggers.
User One 35User One 35
But without Visualforce can we do with triggers, they ask me to do with triggers not with Visualforce