public class testPageController{ public List<sObjectWrapper> objList {get;set;} public testPageController(){ objList = new List<sObjectWrapper>(); List<Account> acc = [select id,name,(select id,name from opportunities),(select id,name from contacts) from account where id =: '00128000003Kgdj' LIMIT 1]; for(Contact con : acc[0].contacts){ objList.add(new sObjectWrapper(con)); } for(Opportunity opp : acc[0].opportunities){ objList.add(new sObjectWrapper(opp)); } }
public class sObjectWrapper{ public SObject obj{get;set;}
public sObjectWrapper (SObject obj){ this.obj = obj; } } }
This will display all the opportunites and contacts related to a account with Id = '00128000003Kgdj'. You can easliy make it dynamic. If you dont want the wrapper class, you can easily create 2 list , 1 for contact and 2nd for opportunity and loop through them on page and can display them.
Apex Class :
public class testPageController{
public List<sObjectWrapper> objList {get;set;}
public testPageController(){
objList = new List<sObjectWrapper>();
List<Account> acc = [select id,name,(select id,name from opportunities),(select id,name from contacts) from account where id =: '00128000003Kgdj' LIMIT 1];
for(Contact con : acc[0].contacts){
objList.add(new sObjectWrapper(con));
}
for(Opportunity opp : acc[0].opportunities){
objList.add(new sObjectWrapper(opp));
}
}
public class sObjectWrapper{
public SObject obj{get;set;}
public sObjectWrapper (SObject obj){
this.obj = obj;
}
}
}
VF Page :
<apex:page controller="testPageController">
<apex:repeat value="{!objList}" var="obj">
{!obj.obj['name']}
</apex:repeat>
</apex:page>
This will display all the opportunites and contacts related to a account with Id = '00128000003Kgdj'. You can easliy make it dynamic. If you dont want the wrapper class, you can easily create 2 list , 1 for contact and 2nd for opportunity and loop through them on page and can display them.