function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajesh NRajesh N 

List of Different Types

I'm looking to create a single list of records of various sObject types using apex to display to a custom visualforce page. I'm wondering if there is a way to combine multiple objects (case, opportunity, account, etc) within a single list. If not, how do you append the second object to the first in a list using visualforce code? Is there a best practice?
Best Answer chosen by Rajesh N
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi,

You will not be able to display it as a sObject on vf page. Visualforce page will not be able to interprete sObject type at runtime.  Also it is quite possible that you might not have same field on all objects.

If you are planning to view differnt object properties on vf page, make use of warpper class to warpp the properties and display it on vf page.
Here is example how you can use wrapper to dispaly various object records :

<apex:page controller="myClass">
    <apex:pageBlock title="Accounts And Contacts">
        <apex:pageBlockTable value="{!lstWrapper}" var="oWrap">
            <apex:column value="{!oWrap.Id}"/>
            <apex:column value="{!oWrap.Name}"/>
            <apex:column value="{!oWrap.sObjectType}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


/********************* Controller ************************/

public class myClass{
    public list<myWrapper> lstWrapper {get;set;}
    public myClass(){
        lstWrapper = new list<myWrapper>();
        
        list<Account> lstAcc = new list<Account>([select id, name from account]);
        
        for(Account oAcc:lstAcc){
            lstWrapper.add(new myWrapper(oAcc.id, oAcc.name, 'Account'));
        }
        
        list<Contact> lstCon = new list<Contact>([select id, name from Contact]);
        
        for(Contact oCon:lstCon){
            lstWrapper.add(new myWrapper(oCon.id, oCon.name, 'Contact'));
        }
    }
    
    public class myWrapper{
        public String Id{get;set;}
        public String Name{get;set;}
        public String sObjectType{get;set;}
        public myWrapper(String Id, String Name, String sObjectType){
            this.Id = Id;
            this.Name = Name;
            this.sObjectType = sObjectType;
        }
    }
}


Thanks,
N.J

All Answers

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi ,

I have answered your question in https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=906F0000000AgTRIA0 (https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=906F0000000AgTRIA0) thread
Grazitti TeamGrazitti Team
Hi Rajesh,

You can achieve this with the help of "Sobject" or "Generic object" of  salesforce.

Please see the links below for reference:
http://www.salesforce.com/us/developer/docs/apex_workbook/Content/apex6_1.htm
https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SObjects_accessing_fields.htm

Please mark as best answer if it solves your problem.

Regards,
Grazitti Team,
www.grazitti.com
Rajesh NRajesh N
Hi Grazitti Team,

Unable to display them in VF page.

Controller
public class Innerlst1 {

    public list<sObject> lstObject { get; set; }

    public Innerlst1(){
        list<sObject> lstObject = new list<sObject>();
       
        Account a = new Account();
        sObject s = [SELECT Id, Name FROM Account LIMIT 1] ;
        lstObject.add((Account)s);
        contact c = new contact();
        sObject s1 = [SELECT Id, lastname FROM contact LIMIT 1];
        lstObject.add((contact)s1);
        
    }
   
}

VF page
----------
<apex:page controller="Innerlst1" >
    <apex:pageBlock >
        <apex:pageBlockTable value="{!lstObject}" var="lst">
            <apex:column value="{!lst.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


Thanks 
Rajesh.
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi,

You will not be able to display it as a sObject on vf page. Visualforce page will not be able to interprete sObject type at runtime.  Also it is quite possible that you might not have same field on all objects.

If you are planning to view differnt object properties on vf page, make use of warpper class to warpp the properties and display it on vf page.
Here is example how you can use wrapper to dispaly various object records :

<apex:page controller="myClass">
    <apex:pageBlock title="Accounts And Contacts">
        <apex:pageBlockTable value="{!lstWrapper}" var="oWrap">
            <apex:column value="{!oWrap.Id}"/>
            <apex:column value="{!oWrap.Name}"/>
            <apex:column value="{!oWrap.sObjectType}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>


/********************* Controller ************************/

public class myClass{
    public list<myWrapper> lstWrapper {get;set;}
    public myClass(){
        lstWrapper = new list<myWrapper>();
        
        list<Account> lstAcc = new list<Account>([select id, name from account]);
        
        for(Account oAcc:lstAcc){
            lstWrapper.add(new myWrapper(oAcc.id, oAcc.name, 'Account'));
        }
        
        list<Contact> lstCon = new list<Contact>([select id, name from Contact]);
        
        for(Contact oCon:lstCon){
            lstWrapper.add(new myWrapper(oCon.id, oCon.name, 'Contact'));
        }
    }
    
    public class myWrapper{
        public String Id{get;set;}
        public String Name{get;set;}
        public String sObjectType{get;set;}
        public myWrapper(String Id, String Name, String sObjectType){
            this.Id = Id;
            this.Name = Name;
            this.sObjectType = sObjectType;
        }
    }
}


Thanks,
N.J
This was selected as the best answer
Rajesh NRajesh N
Looking for this confirmation  " not be able to display it as a sObject on vf page". Got it now. Thanks N.J.

Thanks 
Rajesh.