+ Start a Discussion
Tanushree DuttaTanushree Dutta 

How can I display records from two different objects by putting them in the same list beside a radio button in salesforce

Hi,

I am a newbee to salesforce and I am working on a requirement where I need to display all the records of Contacts and Locations belonging to an Account in a single table format with radio button to choose any 1 record out of Contacts and Locations.

My concern is I have to query separately to get the Contacts and Locations records. So I was thinking if we can merge two lists of separate objects or can we have a map of two different List of objects. Please do reply.
Thanks in Advance.
Best Answer chosen by Tanushree Dutta
JethaJetha
Here I used two different object Lead and Contact: 
<apex:page controller="myclass" >
    <apex:form id="fm">
        <apex:pageBlock >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Select" reRender="pb,fm"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">     
                <apex:pageblocktable value="{!records }" var="wrapper" id="pb" >
                    <apex:column width="25px">                        
                        <apex:inputCheckbox value="{!wrapper.isSelected}"/>
                    </apex:column>
                    <apex:column value="{!wrapper.type}"/>
                    <apex:column value="{!wrapper.id}"/>
                    <apex:column value="{!wrapper.name}"/>
                    <apex:column value="{!wrapper.email}"/>
                    <apex:column value="{!wrapper.phone}"/>
                </apex:pageblocktable>
                <apex:panelGrid columns="7" id="cg">
                    <apex:commandButton status="fetchStatus" reRender="pb" value="first" action="{!first}" disabled="{!!hasPrevious}" title="First Page"/>
                    <apex:commandButton status="fetchStatus" reRender="pb, cg" value="previous" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page"/>
                    <apex:commandButton status="fetchStatus" reRender="pb, cg" value="Next" action="{!next}" disabled="{!!hasNext}" title="Next Page" />
                    <apex:commandButton status="fetchStatus" reRender="pb" value="Last" action="{!last}" disabled="{!!hasNext}" title="Last Page"/>
                </apex:panelGrid>
            </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>
</apex:page>


Your Class:
 
public class myclass
{
    public ApexPages.StandardSetController lee{get;set;}  
    public ApexPages.StandardSetController con{get;set;}

    public List<LeadWrapperCls> records{get;set;}
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}

    public myclass()
    {  
        records = new List<LeadWrapperCls>();
        lee = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, LastName, MobilePhone,Email FROM Lead]));    
        con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, LastName, MobilePhone,Email FROM Contact]));  
      
        lee.setPageNumber(1);     
        lee.setPageSize(5);
        con.setPageSize(5);
        noOfRecords = lee.getResultSize() + con.getResultSize();
        getrecords();
    }
  
    public List<LeadWrapperCls> getrecords()
    {
        records = new List<LeadWrapperCls>();  
        for(Lead category : (List<Lead>)lee.getRecords())
        {
            records.add(new LeadWrapperCls(category));
            system.debug('wrapper lead list' +records );                 
        }                     
 
        for(Contact category : (List<Contact>)con.getRecords())
        {
            records.add(new LeadWrapperCls(category));
            system.debug('wrapper contact list' +records );
        }
        return records ;
    }
    
    public Boolean hasNext 
    {
        get{
        return lee.getHasNext();
        }
        set;
    }
    
    public Boolean hasPrevious 
    {
        get{
        return lee.getHasPrevious();
        }
        set;
    }
 
    public Integer pageNumber 
    {
        get {
            return lee.getPageNumber();
        }
        set;
    }
 
    public void first() 
    {
        lee.first();
        getrecords();
    }
 
    public void last() 
    {
        lee.last();
        getrecords();
    }
 
    public void previous() 
    {
        lee.previous();
        getrecords();
    }
 
    public void next() 
    {
        lee.next();
        getrecords();
    }   

    public class LeadWrapperCls
    {
        public Boolean isSelected {get;set;}
        public Id  id{get;set;}
        public string name{get;set;}
        public string opp{get;set;}
        public String email{set;get;}
        public String phone{set;get;}
        public String type{set;get;}   
        
        public LeadWrapperCls(Lead llead)
        {
            this.id = llead.Id;  
            this.name=llead.LastName;
            this.email=llead.Email;
            this.phone=llead.MobilePhone;     
            isSelected=false;
            type = 'Lead';
        }
       
        public LeadWrapperCls(Contact cCon)
        {
            this.id = cCon.Id;  
            this.name=cCon.LastName;
            this.email=cCon.Email;
            this.phone=cCon.MobilePhone;     
            isSelected=false;
            type = 'Contact';
        }
    }
}

 

All Answers

JethaJetha
You can achieve this functionality using Wrapper class and then display the wrapper class on the Page.
Tanushree DuttaTanushree Dutta
Thank you so much for replying Jetha. 
But can you please explain this with any example if possible?
I'm not very comfortable with wrapper syntax actually.

Thank you.
JethaJetha
Here I used two different object Lead and Contact: 
<apex:page controller="myclass" >
    <apex:form id="fm">
        <apex:pageBlock >
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Select" reRender="pb,fm"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">     
                <apex:pageblocktable value="{!records }" var="wrapper" id="pb" >
                    <apex:column width="25px">                        
                        <apex:inputCheckbox value="{!wrapper.isSelected}"/>
                    </apex:column>
                    <apex:column value="{!wrapper.type}"/>
                    <apex:column value="{!wrapper.id}"/>
                    <apex:column value="{!wrapper.name}"/>
                    <apex:column value="{!wrapper.email}"/>
                    <apex:column value="{!wrapper.phone}"/>
                </apex:pageblocktable>
                <apex:panelGrid columns="7" id="cg">
                    <apex:commandButton status="fetchStatus" reRender="pb" value="first" action="{!first}" disabled="{!!hasPrevious}" title="First Page"/>
                    <apex:commandButton status="fetchStatus" reRender="pb, cg" value="previous" action="{!previous}" disabled="{!!hasPrevious}" title="Previous Page"/>
                    <apex:commandButton status="fetchStatus" reRender="pb, cg" value="Next" action="{!next}" disabled="{!!hasNext}" title="Next Page" />
                    <apex:commandButton status="fetchStatus" reRender="pb" value="Last" action="{!last}" disabled="{!!hasNext}" title="Last Page"/>
                </apex:panelGrid>
            </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>
</apex:page>


Your Class:
 
public class myclass
{
    public ApexPages.StandardSetController lee{get;set;}  
    public ApexPages.StandardSetController con{get;set;}

    public List<LeadWrapperCls> records{get;set;}
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}

    public myclass()
    {  
        records = new List<LeadWrapperCls>();
        lee = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, LastName, MobilePhone,Email FROM Lead]));    
        con = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, LastName, MobilePhone,Email FROM Contact]));  
      
        lee.setPageNumber(1);     
        lee.setPageSize(5);
        con.setPageSize(5);
        noOfRecords = lee.getResultSize() + con.getResultSize();
        getrecords();
    }
  
    public List<LeadWrapperCls> getrecords()
    {
        records = new List<LeadWrapperCls>();  
        for(Lead category : (List<Lead>)lee.getRecords())
        {
            records.add(new LeadWrapperCls(category));
            system.debug('wrapper lead list' +records );                 
        }                     
 
        for(Contact category : (List<Contact>)con.getRecords())
        {
            records.add(new LeadWrapperCls(category));
            system.debug('wrapper contact list' +records );
        }
        return records ;
    }
    
    public Boolean hasNext 
    {
        get{
        return lee.getHasNext();
        }
        set;
    }
    
    public Boolean hasPrevious 
    {
        get{
        return lee.getHasPrevious();
        }
        set;
    }
 
    public Integer pageNumber 
    {
        get {
            return lee.getPageNumber();
        }
        set;
    }
 
    public void first() 
    {
        lee.first();
        getrecords();
    }
 
    public void last() 
    {
        lee.last();
        getrecords();
    }
 
    public void previous() 
    {
        lee.previous();
        getrecords();
    }
 
    public void next() 
    {
        lee.next();
        getrecords();
    }   

    public class LeadWrapperCls
    {
        public Boolean isSelected {get;set;}
        public Id  id{get;set;}
        public string name{get;set;}
        public string opp{get;set;}
        public String email{set;get;}
        public String phone{set;get;}
        public String type{set;get;}   
        
        public LeadWrapperCls(Lead llead)
        {
            this.id = llead.Id;  
            this.name=llead.LastName;
            this.email=llead.Email;
            this.phone=llead.MobilePhone;     
            isSelected=false;
            type = 'Lead';
        }
       
        public LeadWrapperCls(Contact cCon)
        {
            this.id = cCon.Id;  
            this.name=cCon.LastName;
            this.email=cCon.Email;
            this.phone=cCon.MobilePhone;     
            isSelected=false;
            type = 'Contact';
        }
    }
}

 
This was selected as the best answer
Tanushree DuttaTanushree Dutta
Thank you Jetha..

But I have a small doubt in the code.

lee.setPageNumber(1);    
lee.setPageSize(5);
con.setPageSize(5);
noOfRecords = lee.getResultSize() + con.getResultSize();
getrecords();


I am not getting the use "setPageNumber" and "setPageSize" syntax.

Can you please explain that?

Thank you again...