I am trying to display all child records related to a parent account, but I want to display it on the child visualforce page, not on the parent's. Here is my code. Site Visit1 is the child and Account is the parent Object. Currently I am hard coding the account Id in my controller. But I would like to be able to somehow populate accountId here automatically.

When someone is on a siteVisit page, should see all the child of the same Account of which current site visit is a child of. Lets say We are looking at siteVisit A which has Account B, I wanted to show all the other children of Account B on the siteVisit A page. (I thought can be done by vf page) but cant figure out how to bind the id of account here. BTW doctor_office is the master field (Account) on the site visit object.

Controller extension

public class extendSiteVisit {
    public List<Site_Visit1__c> sites{get; set;}
    public extendSiteVisit(ApexPages.StandardSetController controller) {
        sites = [select id, Time_and_Date__c, name, AccountId__c from site_visit1__c where doctor_office__r.Id='0013600000EWmkB'];


Visualforce Page

<apex:page standardController="Site_Visit1__c" extensions="extendSiteVisit"   recordSetVar="sites">
    <apex:pageBlock >
        <apex:pageBlockTable var="s" value="{!sites}">
            <apex:column headerValue="Name of Visit">
                <apex:outputField value="{! s.Name}"/>
            <apex:column headerValue="Date of Visit">
                <apex:outputField value="{! s.Time_and_Date__c}"/>
  • 2
    What is the issue? Are you sure there are records being returned. On the surface it looks like it should work as expected as long as there are records returned. To get the ID of the Account, simply use the value of the Account ID from the Site_Visit1__c object that you get as part of the standard controller. – Eric Aug 21 '16 at 21:46
  • Thanks Eric, I got it did the same . First queried for Site_Visist_1__c along with AccountId and then queried for all the sitevisits for the same accountId. – P.Shri Aug 23 '16 at 11:18

You can do what you want without apex. Try using the <apex:relatedList> tag.

<apex:page standardController="Site_Visit__c" recordSetVar="visits">
    <apex:relatedList subject="{!Site_Visit__c.Doctor_Offiice__c}" list="Site_Visits__r" />

You have a bit less control over which fields to display but you have some.

As for the list attribute, I took a guess at its API Name. You can double check via Execute Anonymous as follows:

for (ChildRelationship relation : SObjectType.Account.getChildRelationships())
    if (relation.getChildSObject() == Site_Visit__c.sObjectType)

Try updating your query to where doctor_office__r.id =: apexpages.currentpage().getparameters().get('id');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.