+ Start a Discussion
prati@salesforceprati@salesforce 

Displaying all child records related to a parent on the child detail page

Hello everybody,
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.  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.Below is my code:
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']; } }

Vf 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> <apex:column headerValue="Date of Visit"> <apex:outputField value="{! s.Time_and_Date__c}"/> </apex:column> </apex:pageBlockTable> </apex:pageBlock> </apex:page> 
Best Answer chosen by prati@salesforce
Keyur  ModiKeyur Modi
Hi prati, 

I have tried the same thing with below code :- 
<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> 
<apex:column headerValue="Date of Visit"> 
<apex:outputField value="{! s.Time_and_Date__c}"/> 
</apex:column> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:page> 

public class extendSiteVisit { 
public List<Site_Visit1__c> sites{get; set;} 
public Opportunity Site_Visit1__c{get; set;}

	public extendSiteVisit(ApexPages.StandardSetController controller) {
		 currentRecord = [SELECT Id, Name, AccoutID FROM site_visit1__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; // this will return current record object(child) detail from that you can use parent Id (here Account is parent so accoutId)
		 
	 sites = [SELECT id, Time_and_Date__c, name, AccountId FROM site_visit1__c WHERE AccountId=:currentRecord.accountId]; 
	} 

}

with the above code , current reocrd will return current site record detail with referance to current site detail you can get accoutId (parrentId) for that you can query all site related to that Account.

Please let me know if you need any help on this,

Thanks,
Keyur Modi

All Answers

Keyur  ModiKeyur Modi
Hi prati, 

I have tried the same thing with below code :- 
<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> 
<apex:column headerValue="Date of Visit"> 
<apex:outputField value="{! s.Time_and_Date__c}"/> 
</apex:column> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:page> 

public class extendSiteVisit { 
public List<Site_Visit1__c> sites{get; set;} 
public Opportunity Site_Visit1__c{get; set;}

	public extendSiteVisit(ApexPages.StandardSetController controller) {
		 currentRecord = [SELECT Id, Name, AccoutID FROM site_visit1__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; // this will return current record object(child) detail from that you can use parent Id (here Account is parent so accoutId)
		 
	 sites = [SELECT id, Time_and_Date__c, name, AccountId FROM site_visit1__c WHERE AccountId=:currentRecord.accountId]; 
	} 

}

with the above code , current reocrd will return current site record detail with referance to current site detail you can get accoutId (parrentId) for that you can query all site related to that Account.

Please let me know if you need any help on this,

Thanks,
Keyur Modi
This was selected as the best answer
prati@salesforceprati@salesforce
Thank you for your reply.In your case is the Opportunity parent of Site Visit? and do i need to use Doctor_office__r.Id to represent accountId since doctor office is the name of the relation from site visit to Account?
prati@salesforceprati@salesforce
I did some modifications according to my fields and it works now.... But I wanted toi embed this on my site Visit page layout but when i go to visualforce pages option on edit layout of site visit page, I dont see the vf page which I just created. I was pretty sure , it was posible but I dont know where am I making mistake. Any ideas?
prati@salesforceprati@salesforce
Thank you , i figured it out. Actually we can only use standard controller or extensions on a standard page layout but earlier I was using standard Set controller.