Tuesday, 4 July 2017
Sunday, 7 May 2017
parent to child soqls
Parent to Child
SOQl
If
two objects are connected by relationship field like (master detail or lookup
relation ) then we can write a soql to fetch child records from parent object
Ex: Account and
contact object are connected by using lookup relation
Ex: Customer object
and Transaction object are connected by using master-detail relation
Example Program to
display Account records along with corresponding contact records
public class ParentChild {
public List<Account> accs{set;get;}
public List<Customer__c> cust{set;get;}
public
ParentChild(){
accs=[select Name,Industry,(select
id,lastname,firstname from Contacts)
from Account];
cust=[select
Customer_Name__c,AType__c ,(select Type__c,Amount__c from
Transactions__r) from Customer__c];
}
}
VF page :
<apex:page controller="ParentChild">
<apex:pageBlock title="Account and Contacts">
<apex:pageBlockTable value="{!accs}" var="a">
<apex:column value="{!a.Name}" />
<apex:column value="{!a.industry}" />
<apex:column headerValue="Contacts">
<apex:repeat value="{!a.contacts}" var="b">
{!b.lastname}
-{!b.firstname}<br/>
</apex:repeat>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title="Customers and Transactions">
<apex:pageBlockTable value="{!cust}" var="a">
<apex:column value="{!a.Customer_Name__c}" />
<apex:column value="{!a.AType__c}" />
<apex:column headerValue="Transactions">
<apex:repeat value="{!a.Transactions__r}" var="b">
{!b.Type__c}
-{!b.amount__c}<br/>
</apex:repeat>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Subscribe to:
Posts (Atom)