SOQL Scenario -3
Parent to child Relationship
fetch the Contact fields from Account
// parent to child relationship
public class SoqlQuery3 {
public List<Account> acc{set;get;}
public SoqlQuery3(){
acc=[select id, name, phone,(select id, lastname, firstname from contacts) from Account];
}
}
<apex:page controller="SoqlQuery3" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!acc}" var="a">
<apex:column headerValue="Acc Name" value="{!a.name}" />
<apex:column headerValue="Acc Phone" value="{!a.phone}" />
<apex:column headerValue="Con Details">
<table>
<apex:repeat value="{!a.contacts}" var="b">
<tr>
<td>{!b.lastname}</td><td>{!b.firstname}</td>
</tr>
</apex:repeat>
</table>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
fetch the Contact fields from Account
// parent to child relationship
public class SoqlQuery3 {
public List<Account> acc{set;get;}
public SoqlQuery3(){
acc=[select id, name, phone,(select id, lastname, firstname from contacts) from Account];
}
}
<apex:page controller="SoqlQuery3" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!acc}" var="a">
<apex:column headerValue="Acc Name" value="{!a.name}" />
<apex:column headerValue="Acc Phone" value="{!a.phone}" />
<apex:column headerValue="Con Details">
<table>
<apex:repeat value="{!a.contacts}" var="b">
<tr>
<td>{!b.lastname}</td><td>{!b.firstname}</td>
</tr>
</apex:repeat>
</table>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Comments
Post a Comment