You need to sign in to do that
Don't have an account?
User One 35
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>
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>
Simply: Triggers are not meant to control the UI elements.
The purpose of triggers is to automate the process upon the events like insert, update, delete and undelte. they only handle the data in ur system. No other business.
It can also be said "There is no other way you can use trigger logic without making a DML."
That is why the syntax has a mendatory aspect of BEFORE_EVENT and AFTER_EVENT.
Things TRIGGERS can do upon those DML statements, some:
- It can create related data or any data for that matters,
- It can Query the database and update data, not to mention the deletes etc,
- It is synchronous
The list goes on...Things TRIGGERS cannot do, some:
- Redirect the user to different page based on value
- Callout during the save/ delete, so developers use the workaround
- Show and Hide certain data elements on the record (Your case here)
Salesforce has given other solutions for other challenges.If you want to show all the child records on parents, bring them all on pagelayout. if you want to show more records, click on "show more" link below on the related list.
Let me know what is the business case here.
PrabhaN
All Answers
"What you have is an apex class (Controller/ extention class), NOT trigger."
It is easy to identify the difference, triggers have the word "trigger" in the first line and so is for class.
Now getting into the details, There's couple of ways you can do this.
For example, if you want to display Opportunities, Task and all other objects in the VF page, you need to have the same patterns repeated as you did for Contacts.
PrabhaN
- One more thing if you are going with option1: Your URL will have to have 'id' instead of 'acId'. StandardController detects 'id' and takes care of the rest for you.
PrabhaNYes, I am using class not trigger my question how to use a trigger to display all child records should display in the parent object. forget about Visualforce page and apex code which I mention. I want to start another way that using triggers
PrabhaN
Simply: Triggers are not meant to control the UI elements.
The purpose of triggers is to automate the process upon the events like insert, update, delete and undelte. they only handle the data in ur system. No other business.
It can also be said "There is no other way you can use trigger logic without making a DML."
That is why the syntax has a mendatory aspect of BEFORE_EVENT and AFTER_EVENT.
Things TRIGGERS can do upon those DML statements, some:
- It can create related data or any data for that matters,
- It can Query the database and update data, not to mention the deletes etc,
- It is synchronous
The list goes on...Things TRIGGERS cannot do, some:
- Redirect the user to different page based on value
- Callout during the save/ delete, so developers use the workaround
- Show and Hide certain data elements on the record (Your case here)
Salesforce has given other solutions for other challenges.If you want to show all the child records on parents, bring them all on pagelayout. if you want to show more records, click on "show more" link below on the related list.
Let me know what is the business case here.
PrabhaN
In Visualforce page Error: Unknown property 'String.Name'
Apex Error: Compile Error: Unexpected token 'accID'. at line 9 column 4
Apex:
public class ContactsRecordsToAccounts{
Public Id accID;
public List<Contact> contactList{get;set;}
public List<Opportunities>opprList{get;set;}
public ContactsRecordsToAccounts(){
contactList = new List<Contact>();
opprList = new List<Opportunities>
accID= ApexPages.currentPage().getParameters().get('acId');
contactList = [SELECT FirstName,LastName,Email,Phone FROM Contact WHERE AccountID = : accID];
opprList = [SELECT Name, Account, Owner Type FROM Opportunities WHERE AcountID = :accID];
}
}
Visualforce Page:
<apex:page controller="ContactsRecordsToAccounts" sidebar="false" showHeader="false">
<apex:pageBlock >
<apex:pageblocksection 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:pageblocksection>
<apex:pageblocksection title="Opportunities">
<apex:pageblocktable value="{!opprList}" var="op">
<apex:column value="{!op.Name}"/>
<apex:column value="{!op.Account}"/>
<apex:column value="{!op.Owner}"/>
<apex:column value="{!op.Type}"/>
</apex:pageblocktable>
</apex:pageblocksection>
</apex:pageBlock>
</apex:page>
Also, The query " opprList = [SELECT Name, Account, Owner Type FROM Opportunities WHERE AcountID = :accID]; " needs to read as follows:
opprList = [SELECT Name, Account, Ownerid, Type FROM Opportunities WHERE AcountID = :accID];
PrabhaN
Is there any chance to display record count in parent object using Triggers, if yes please help me with that also. I am new to coding due to that too many questions for you
Here is an example I found on forums, modified for ur requirement:
Trigger on Opportunity:
The class:
PrabhaN