You need to sign in to do that
Don't have an account?

Show related contacts for each account
Hi friends
I have a requirement where for every account name I need to show the related contact details in a separate visual force page.
For this I have written code to display all account names with a link button which will fetch related contact details.
But I am stuck at the code part in the second VF page.
VF_GetAllAccounts
<apex:page controller="DisplayAccountNamesController">
<apex:form >
<apex:messages />
<apex:pageBlock >
<apex:pageBlockTable value="{!Accounts}"
var="a">
<apex:column value="{!a.Name}"/>
<apex:column value="{!a.Phone}"/>
<apex:column width="12%">
<apex:commandLink action="{!OpenAccount}">
<apex:param name="SelectedAccountId"
value="{!a.Id}"
assignTo="{!SelectedAccountId}"/>
<h1>Open</h1>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
apex class1:
public class DisplayAccountNamesController
{
public string SelectedAccountId{get;set;}
public DisplayAccountNamesController ()
{
}
public list<Account> getAccounts()
{
return [select ID,Name from Account order By Name Limit 20];
}
public PageReference OpenAccount()
{
return new PageReference('/' + SelectedAccountId);
}
}
Now I create the second VF page
VF_Page2
<apex:dataTable value="{!contacts}" var="con" id="theTable">
<apex:facet name="caption">table caption</apex:facet>
<apex:facet name="header">table header</apex:facet>
<apex:facet name="footer">table footer</apex:facet>
<apex:column >
<apex:outputText value="{!con.name}"/>
</apex:column>
<apex:column >
<apex:outputText value="{!con.LastName}"/>
</apex:column>
</apex:dataTable>
In apex class1, I have SelectedAccountID by which I can fetch related contacts but where do I code the soql query. because when I save this VF_page2
it will create another apex class .
Please let me know
regards
sonali verma
I have a requirement where for every account name I need to show the related contact details in a separate visual force page.
For this I have written code to display all account names with a link button which will fetch related contact details.
But I am stuck at the code part in the second VF page.
VF_GetAllAccounts
<apex:page controller="DisplayAccountNamesController">
<apex:form >
<apex:messages />
<apex:pageBlock >
<apex:pageBlockTable value="{!Accounts}"
var="a">
<apex:column value="{!a.Name}"/>
<apex:column value="{!a.Phone}"/>
<apex:column width="12%">
<apex:commandLink action="{!OpenAccount}">
<apex:param name="SelectedAccountId"
value="{!a.Id}"
assignTo="{!SelectedAccountId}"/>
<h1>Open</h1>
</apex:commandLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
apex class1:
public class DisplayAccountNamesController
{
public string SelectedAccountId{get;set;}
public DisplayAccountNamesController ()
{
}
public list<Account> getAccounts()
{
return [select ID,Name from Account order By Name Limit 20];
}
public PageReference OpenAccount()
{
return new PageReference('/' + SelectedAccountId);
}
}
Now I create the second VF page
VF_Page2
<apex:dataTable value="{!contacts}" var="con" id="theTable">
<apex:facet name="caption">table caption</apex:facet>
<apex:facet name="header">table header</apex:facet>
<apex:facet name="footer">table footer</apex:facet>
<apex:column >
<apex:outputText value="{!con.name}"/>
</apex:column>
<apex:column >
<apex:outputText value="{!con.LastName}"/>
</apex:column>
</apex:dataTable>
In apex class1, I have SelectedAccountID by which I can fetch related contacts but where do I code the soql query. because when I save this VF_page2
it will create another apex class .
Please let me know
regards
sonali verma
Try something like this. You know the account id, then its straight forward you can use related list to get the contact.
apex:page standardController="Account">
<apex:pageBlock> You're looking at some related lists for {!account.name}:
<apex:relatedList list="Contacts"> <apex:facet name="header">Titles can be overriden with facets</apex:facet> </apex:relatedList>
</apex:page>
Thanks.
Vetri
I have written the code. But what I require is for each account it should show related contact records.
Please let me know where I can improvise.
AccountName{!Account.Name}
Last Name
First Name
public class My_Account
{
private final Account accn;
public List con;
public My_Account(ApexPages.StandardController stdcontroller)
{
this.accn=(Account)stdcontroller.getRecord();
}
public list getcontVals()
{
con=new list();
con=[select lastname,firstname from contact where AccountID=:accn.ID and Phone != NULL];
return con;
}
}
the code was not pasted properly. Here is the full one.
AccountName{!Account.Name}
Last Name
First Name
public class My_Account
{
private final Account accn;
public List con;
public My_Account(ApexPages.StandardController stdcontroller)
{
this.accn=(Account)stdcontroller.getRecord();
}
public list getcontVals()
{
con=new list();
con=[select lastname,firstname from contact where AccountID=:accn.ID and Phone != NULL];
return con;
}
}
You don't need to write controller code at all. Please refer the below link
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_relatedList.htm
Please let me know if it helps
Thanks
Vetri
I dont see any output on the page. I removed the apex code and keept only the VF .
Am I missing anything:?
regards
sonali verma
click on below link for perfect answer
http://salesforcecodes.com/display-related-contacts-when-click-on-account-record-using-lightning-component/ (http://salesforcecodes.com/display-related-contacts-when-click-on-account-record-using-lightning-component/" target="_blank)