+ Start a Discussion
Manpreet Singh 13Manpreet Singh 13 

Conditional Display of records on Visualforce page

Dear All,

I have created a visualforce page wherein I am getting all Tasks and Email records from the Case. I am showing them in the chronological order.

I have created another component (not working and need help) on Visualforce page which allow users to either select Tasks or Emails and then click on 'Show' button. Depending on the selection on visualforce page, user would either see Tasks or Emails on the visualforce page. Any help/pointers would help alot.

Related List Selection

*************************************Here is the code of my controller********************************************

public with sharing class CaseEmailExtension {

  private final Case currentCase;

  public CaseEmailExtension(ApexPages.StandardController currentcase) {
    this.currentCase = (Case)currentcase.getRecord();

  public List<EmailMessage> getSortEmails(){
    List <EmailMessage> sortedEmails = new List<EmailMessage>();
    sortedEmails = [SELECT Id, FromAddress, ToAddress, BCCAddress, MessageDate, Subject, HasAttachment, Incoming, TextBody, CreatedBy.Name  
            from EmailMessage where ParentId =: currentCase.Id 
            order by MessageDate DESC ];
    return sortedEmails;

  public List<Task> getTask() {
    List<Task> Task= new List<Task>();
    Task = [SELECT Id, Priority, Subject, Status, Description, CreatedDate, ActivityDate, CreatedBy.Name,Who.Name
            from Task order by CreatedDate DESC]; 
    return Task;  


********************************Here is Visualforce Page code*****************************


<apex:pageblock >

<apex:pageBlockSection id="RelatedList" columns="2" title="Related List Selection" collapsible="true">
                <apex:selectcheckboxes layout="pageDirection">
                    <apex:selectOption itemLabel="Tasks and Activities" itemValue="Tasks"/>
                    <apex:selectOption itemLabel="Emails" itemValue="EMAIL"/>
          <apex:outputPanel layout="none">
          <apex:commandButton value="Show" />

Mudasir WaniMudasir Wani
Your page should be 


<apex:form id = "formid">

<apex:outputPanel  id="firstPanel">
       <apex:pageBlock >
        <apex:pageblockTable value="{!TasksRecordList}" var="wrapRec" rendered="{!showTasks}">
       <apex:column value="{!wrapRec.accName}" />
       <apex:column value="{!wrapRec.oppName}"/>
<apex:outputPanel  id="secondPanel">
       <apex:pageBlock >
        <apex:pageblockTable value="{!EmailRecList}" var="wrapRec" rendered="{!showEmail}">
       <apex:column value="{!wrapRec.accName}" />
       <apex:column value="{!wrapRec.oppName}"/>

<apex:commandButton value="Show"  reRender="firstPanel,secondPanel"/>
Your Class
public class className{
	public className(){
		showTasks = false;
		showEmail = false;
	public boolean showTasks{get;set;}
	public boolean showEmail{get;set;}
	//Say you have Task object 
	public list<Task> TasksRecordList{get;set;}
	//If you have email object 
	public list<Email> EmailRecList{get;set;}
	public void show(){
		//based on what radio was selected you can do the things
		//If show radio was selected from your page and clicked show
		if(showEmailCheckbox == true){
			showEmail = true;
			//fetch Email list 
			EmailRecList = [Select id,name from Email];
		else if(showTaskCheckBox == true){
			showTasks = true;
			//fetch Task list 
			TasksRecordList = [Select id,name from Task];

Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Mudasir WaniMudasir Wani
Correct page code to add controller 

<apex:page controller="className">