+ Start a Discussion
RajashriRajashri 

Display Data in single table

Hi,

I have two pageblock table to display the data from two different variables.Can anyone please guide me how can i show the data in single table.

Below is my code

<apex:page StandardController="Campaign"  extensions="singleListView">
<apex:form >
        <apex:pageBlock mode="maindetail" >
                      <apex:pageblocktable value="{!CampaignMembers}" var="cm">
                 
                 <apex:column headerValue="Phone">
                           <apex:repeat value="{!cm.CampaignMembers}" var="cpm"> 
                             <apex:outputfield value="{!cpm.Lead.Phone}" />
                              </apex:repeat>
                               </apex:column>
                                                   
            </apex:pageblocktable>
 
            <apex:pageBlockTable value="{!ContactMembers}" var="cmc">
                            <apex:column headerValue="Phone">
                           <apex:repeat value="{!cmc.CampaignMembers}" var="cpmc"> 
                            <apex:outputfield value="{!cpmc.Contact.Phone}" rendered="{!cpmc.Contact.Phone != null}"/>
                            </apex:repeat>
                            </apex:column>
                          </apex:pageBlockTable> 
              
                        </apex:pageBlock>   
    
    </apex:form>
</apex:page>
Best Answer chosen by Rajashri
Sumitkumar_ShingaviSumitkumar_Shingavi
Here you go with one more revision
public with sharing class singleListView {

    public Campaign camp {get; set; }
	public List<MemberWrapper> lMemberWrappers {get; set;}
	
    public singleListView(ApexPages.StandardController controller) {
		camp = (Campaign)controller.getRecord();
        lMemberWrappers = new List<MemberWrapper>();
		getCampaignMembers();
		getContactMembers();
		for(Lead ld : CampaignMembers) {
			lMemberWrappers.add(new MemberWrapper(ld.Phone));
		}
		for(Contact con : ContactMembers) {
			lMemberWrappers.add(new MemberWrapper(con.Phone));
		}
    }
  
    private List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
		CampaignMembers = [Select Id,Name, Phone, (Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,Lead.Name,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
		(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
		From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
		return CampaignMembers;
	}     
         
    private List<Contact> ContactMembers;
    public List<Contact> getContactMembers() {
		ContactMembers=[Select Id,Name, Phone, (Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,LeadID,Lead.Name,ContactID,Lead.Phone,Lead.Email,Contact.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone,Contact.MobilePhone From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
		(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
		From Contact  where Id IN(select ContactId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
        return ContactMembers;
	}
	
	public class MemberWrapper {
		public String Phone {get; set;}
		public MemberWrapper(String Phone) {
			this.Phone = Phone;
		}
	}
}
Hope this helps!

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
You need to little twick and make use of Wrapper Classes here. So, you already have CampaignMembers and ContactMembers lists. You can create a inner class in same controller like below
public class MemberWrapper {
	
	public String Phone {get; set;}
	//Add more fields if you need
	
	public MemberWrapper(String Phone) {
		this.Phone = Phone;
		//Add more fields if you need
	}	
}
And then iterate over like below in VF
<apex:pageBlock mode="maindetail" >
	<apex:pageblocktable value="{!lMemberWrappers}" var="mem">
		<apex:column headerValue="Phone">			
			<apex:outputfield value="{!mem.Phone}" />			
		</apex:column>
</apex:pageblocktable>

So, MemberWrappers is a variable of List<MemberWrapper> just like
List<MemberWrapper> lMemberWrappers = new List<MemberWrapper>();
Hope this helps. If yes, mark it as solution so that it can help other's too who have same problem.
James LoghryJames Loghry

It depends on how you want the data to display, really. There are many different ways a pageblock / pageblock section can be rendered.  Here's a pretty comprehensive list on the differences of each: http://limitexception.com/2014/03/30/visualforce-a-visual-reference-guide/

If you want a simple pageBlock table with the campaign member and contact data then I would suggest on iterating through all the possible campaign members and printing out the campaign members and contact details in a single row.  Below is an example:

 

<apex:pageBlockTable value="{!campaignMembers}" var="cm">
    <apex:column value="{!cm.Campaign.Name}" />
    <apex:column value="{!cm.Contact.Phone}" />
    <apex:column value="{!cm.Lead.Phone}" />
</apex:pageBlockTable>
RajashriRajashri
Thanks Sumit! Below is my code..i am trying to create the Wrapper Class here but i am getting an error that

Unknown property 'CampaignStandardController.lMemberWrappers'

Can you please help? 


My page

 <apex:pageBlock mode="maindetail" >
    <apex:pageblocktable value="{!lMemberWrappers}" var="mem">
        <apex:column headerValue="Phone">           
            <apex:outputfield value="{!mem.Phone}" />           
        </apex:column>
</apex:pageblocktable>

Controller Code

public with sharing class singleListView {

   public Campaign camp {get; set; }
        List<MemberWrapper> lMemberWrappers = new List<MemberWrapper>();
    public singleListView(ApexPages.StandardController controller) {
       camp = (Campaign)controller.getRecord();
         
    }
  
    public List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
   CampaignMembers=[Select Id,Name,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,Lead.Name,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
       return CampaignMembers;
                
         }     
         
    public List<Contact> ContactMembers;
    public List<Contact> getContactMembers() {
     ContactMembers=[Select Id,Name,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,LeadID,Lead.Name,ContactID,Lead.Phone,Lead.Email,Contact.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone,Contact.MobilePhone From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
From Contact  where Id IN(select ContactId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
       return ContactMembers;
                
         }     
         
public class MemberWrapper {
    public String Phone {get; set;}
    public MemberWrapper(String Phone) {
        this.Phone = Phone;
    }  

}

         
      }
Sumitkumar_ShingaviSumitkumar_Shingavi
Here you go:
public with sharing class singleListView {

    public Campaign camp {get; set; }
	public List<MemberWrapper> lMemberWrappers {get; set;}
	
    public singleListView(ApexPages.StandardController controller) {
		camp = (Campaign)controller.getRecord();
        lMemberWrappers = new List<MemberWrapper>();
		getCampaignMembers();
		getContactMembers();
		for(Lead ld : CampaignMembers) {
			lMemberWrappers.add(new MemberWrapper(ld.Phone));
		}
		for(Contact con : ContactMembers) {
			lMemberWrappers.add(new MemberWrapper(con.Phone));
		}
    }
  
    private List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
		CampaignMembers = [Select Id,Name,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,Lead.Name,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
		(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
		From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
		return CampaignMembers;
	}     
         
    private List<Contact> ContactMembers;
    public List<Contact> getContactMembers() {
		ContactMembers=[Select Id,Name,(Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,LeadID,Lead.Name,ContactID,Lead.Phone,Lead.Email,Contact.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone,Contact.MobilePhone From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
		(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
		From Contact  where Id IN(select ContactId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
        return ContactMembers;
	}
	
	public class MemberWrapper {
		public String Phone {get; set;}
		public MemberWrapper(String Phone) {
			this.Phone = Phone;
		}
	}
}
Hope this helps!
 
RajashriRajashri
Thanks Sumit...I have used the controller code sent by you but it is giving me an error that

Content cannot be displayed: SObject row was retrieved via SOQL without querying the requested field: Lead.Phone


Below is my VF page

<apex:page StandardController="Campaign"  extensions="singleListView">
<apex:form >
<apex:pageblocktable value="{!lMemberWrappers}" var="mem">
        <apex:column headerValue="Phone">           
            <apex:outputText value="{!mem.Phone}" /> 
                      
        </apex:column>
</apex:pageblocktable> 
</apex:pageBlock>
Sumitkumar_ShingaviSumitkumar_Shingavi
Here you go with one more revision
public with sharing class singleListView {

    public Campaign camp {get; set; }
	public List<MemberWrapper> lMemberWrappers {get; set;}
	
    public singleListView(ApexPages.StandardController controller) {
		camp = (Campaign)controller.getRecord();
        lMemberWrappers = new List<MemberWrapper>();
		getCampaignMembers();
		getContactMembers();
		for(Lead ld : CampaignMembers) {
			lMemberWrappers.add(new MemberWrapper(ld.Phone));
		}
		for(Contact con : ContactMembers) {
			lMemberWrappers.add(new MemberWrapper(con.Phone));
		}
    }
  
    private List<Lead> CampaignMembers;
    public List<Lead> getCampaignMembers() {
		CampaignMembers = [Select Id,Name, Phone, (Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,Lead.Name,LeadID,ContactID,Lead.Phone,Lead.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone  From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
		(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
		From Lead  where Id IN(select LeadId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
		return CampaignMembers;
	}     
         
    private List<Contact> ContactMembers;
    public List<Contact> getContactMembers() {
		ContactMembers=[Select Id,Name, Phone, (Select id, Campaign.Name,Contact.Phone,Lead.FirstName,Lead.LastName,LeadID,Lead.Name,ContactID,Lead.Phone,Lead.Email,Contact.Email, Lastmodifieddate,Status,CampaignId,Campign_ID__c,Lead.MobilePhone,Contact.MobilePhone From CampaignMembers where CampaignId =:camp.Id and Status != '' ), 
		(Select Subject, Id,lastModifiedDate From ActivityHistories  where lastModifiedDate !=null and Subject !=null order by LastModifiedDate desc limit 1) 
		From Contact  where Id IN(select ContactId from campaignMember where campaignId =:camp.Id ) order by LastModifiedDate desc];
        return ContactMembers;
	}
	
	public class MemberWrapper {
		public String Phone {get; set;}
		public MemberWrapper(String Phone) {
			this.Phone = Phone;
		}
	}
}
Hope this helps!
This was selected as the best answer
RajashriRajashri
Thanks Sumit the code works fine for Phone but i am trying to add fields such as Name, Status then i am getting an errorConstructor Not defined
for the highlighted line..
why so?

for(Lead ld : CampaignMembers) {
            lMemberWrappers.add(new MemberWrapper(ld.Phone),new MemberWrapper(ld.Name) );
           // lMemberWrappers.add(new MemberWrapper(ld.Name));
            
        }
        for(Contact con : ContactMembers) {
            lMemberWrappers.add(new MemberWrapper(con.Phone),new MemberWrapper(con.Name));
           // lMemberWrappers.add(new MemberWrapper(con.Status));
        }


 public class MemberWrapper {
        public String Status {get; set;}
        public String Name {get; set;}
        public String Phone {get; set;}
        public String Mobile {get; set;}
        public String LastModifiedDate {get; set;}
        public MemberWrapper(String Phone,String Name) {
            this.Phone = Phone;
            this.Status = Name;
             
        }
 
Sumitkumar_ShingaviSumitkumar_Shingavi
You have to define constructors as per your requirements and number of field. I will suggest you to have a single constrcutor and add parameters to it appropriately. So, your wrapper class should be like
public class MemberWrapper {
	public String Status {get; set;}
	public String Name {get; set;}
	public String Phone {get; set;}
	public String Mobile {get; set;}
	public String LastModifiedDate {get; set;}
	
	public MemberWrapper(String Status, String Name, String Phone, String Mobile, String LastModifiedDate) {
		this.Status = Status;
		this.Name = Name;
		this.Phone = Phone;
		this.Mobile = Mobile;
		this.LastModifiedDate = LastModifiedDate;
	}
}
And so your will add records in list variable (lMemberWrappers) like below:
lMemberWrappers.add(new MemberWrapper(ld.Status, ld.Name, ld.Phone, ld.Mobile, ld.LastModifiedDate));
Don't forget those new fields in SOQL queries same as Phone field.

PS: Kindly mark appropriate comment as solution which helped you most above.
RajashriRajashri
Thanks a lot sumit....but i want to add the column Subject, LastModified date from Inner Query(Activity History Object) and Status from 
Campaign Members(Object)..How can we do it?
Sumitkumar_ShingaviSumitkumar_Shingavi
Have nested for loop insider original for loop at this moment which iterate over Lead and Contacts and then inside that create MemberWrapper instance. I think, you should brush up a little on Apex Workbook before working on this kind of things as they need good understanding of data structures. Please don't expect the code everytime as I can show you approach and not code everytime. Please select right solution in context of this post.