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
swetha Rao 19swetha Rao 19 

Display accounthistory or contacthistory records in a pageblock table

I want to display accounthistory or contacthistory records in a pageblock table when i select accounthistory or contactHistory from a picklist.
Example:: picklist values accountHistory , if i select this the account history records should display?How?..Can any one help me please..
 
Best Answer chosen by swetha Rao 19
Dhanya NDhanya N
Hi Swetha,

Refer below page and controller.

Page:
<apex:page controller="historiesController">
    <apex:form id="TheForm">
        <apex:selectList value="{!histories}" size="1">
            <apex:selectOptions value="{!items}"/>
            <apex:actionSupport event="onchange" action="{!fetchHistories}" rerender="pbId" />
        </apex:selectList><p/>
        
        <apex:pageBlock title="My Content" id="pbId">        
			<apex:pageBlockTable id="tbId" value="{!lstAccountHistories}" var="item1" rendered="{!lstAccountHistories.size>0}">
				<apex:column value="{!item1.NewValue}"/>
			</apex:pageBlockTable>
			
			<apex:pageBlockTable id="tb2Id" value="{!lstContactHistories}" var="item2" rendered="{!lstContactHistories.size>0}">
				<apex:column value="{!item2.NewValue}"/>
			</apex:pageBlockTable>
		</apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class historiesController {
    
    public String histories { get; set; }    
    public list<ContactHistory> lstContactHistories { get; set;}
    public list<AccountHistory> lstAccountHistories  {get;set;} 
    
    public historiesController() {
        lstContactHistories = new list<ContactHistory>();
        lstAccountHistories  = new list<AccountHistory>();
    } 
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','None'));
        options.add(new SelectOption('AccountHistory','Account History'));
        options.add(new SelectOption('ContactHistory','Contact History'));
        return options;
    }
    
    public void fetchHistories() {
	
        lstContactHistories = new list<ContactHistory>();
        lstAccountHistories  = new list<AccountHistory>();
        
        if(histories == 'AccountHistory') {        
            lstAccountHistories = [Select Id, Field, NewValue, OldValue From AccountHistory limit 10];           
        }
        
        else if(histories == 'ContactHistory') {           
            lstContactHistories = [Select Id,  Field, NewValue, OldValue From ContactHistory limit 10];          
        }
    }
}

Thanks,
Dhanya

All Answers

Dhanya NDhanya N
Hi Swetha,

Refer below page and controller.

Page:
<apex:page controller="historiesController">
    <apex:form id="TheForm">
        <apex:selectList value="{!histories}" size="1">
            <apex:selectOptions value="{!items}"/>
            <apex:actionSupport event="onchange" action="{!fetchHistories}" rerender="pbId" />
        </apex:selectList><p/>
        
        <apex:pageBlock title="My Content" id="pbId">        
			<apex:pageBlockTable id="tbId" value="{!lstAccountHistories}" var="item1" rendered="{!lstAccountHistories.size>0}">
				<apex:column value="{!item1.NewValue}"/>
			</apex:pageBlockTable>
			
			<apex:pageBlockTable id="tb2Id" value="{!lstContactHistories}" var="item2" rendered="{!lstContactHistories.size>0}">
				<apex:column value="{!item2.NewValue}"/>
			</apex:pageBlockTable>
		</apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class historiesController {
    
    public String histories { get; set; }    
    public list<ContactHistory> lstContactHistories { get; set;}
    public list<AccountHistory> lstAccountHistories  {get;set;} 
    
    public historiesController() {
        lstContactHistories = new list<ContactHistory>();
        lstAccountHistories  = new list<AccountHistory>();
    } 
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','None'));
        options.add(new SelectOption('AccountHistory','Account History'));
        options.add(new SelectOption('ContactHistory','Contact History'));
        return options;
    }
    
    public void fetchHistories() {
	
        lstContactHistories = new list<ContactHistory>();
        lstAccountHistories  = new list<AccountHistory>();
        
        if(histories == 'AccountHistory') {        
            lstAccountHistories = [Select Id, Field, NewValue, OldValue From AccountHistory limit 10];           
        }
        
        else if(histories == 'ContactHistory') {           
            lstContactHistories = [Select Id,  Field, NewValue, OldValue From ContactHistory limit 10];          
        }
    }
}

Thanks,
Dhanya
This was selected as the best answer
swetha Rao 19swetha Rao 19
Hey Thank you Dhanya for your reply....But I am getting an error : Illegal assignment from List<ContactHistory> to List<ContactHistory> at line 30 column 13..
Can you please tell me why I am getting this error..
Dhanya NDhanya N
Hi Swetha,
In your organization somewhere you have created a class named "ContactHistory". Thats why the compiler is not able to understand that it is standard Object ContactHistory or the class created by you. Please rename your class, it will work.

If the above doesn't work then try this:  List<Schema.ContactHistory> lstContactHistories {get;set;}
swetha Rao 19swetha Rao 19
Ya I have class with account history and contact history but I deleted both of them but still it is not working?
Dhanya NDhanya N
Once use this and check List<Schema.ContactHistory> lstContactHistories {get;set;} 
swetha Rao 19swetha Rao 19
Hi ,
I need checkbox in that above PageblockTable? Can anyone help me please?
Dhanya NDhanya N
Hi Swetha,

Which field's history you want to track from this vf page?
Only one field's history you can track itseems.

Thanks,
Dhanya
swetha Rao 19swetha Rao 19
kk ....
what about checkbox in pageblock table to delete records?
Dhanya NDhanya N
Then you have to use wrapper class.
Please refer this link you will get some idea to implement in your code.
http://sfdcsrini.blogspot.com/2014/12/adding-and-deleting-rows-dynamically-in.html
swetha Rao 19swetha Rao 19
I created picklist when I selected course the course records should be displayed in a pageblock table...But the records are not displaying in pageblock table?????Can anyone help me please??????...... 

<apex:page controller="DeleteRecords_wrapperClass">
  <apex:form >
    <apex:pageBlock >
      <apex:selectList value="{!SObjects}" size="1">
         <apex:selectOptions value="{!SelectObjects}"/>
      <apex:actionSupport event="OnChange" action="{!DisplayRecords}" reRender="Pgrf" />
      </apex:selectList>
    </apex:pageBlock> 
        <apex:pageBlock Id="Pgrf" rendered="{!Hide}">  
            <apex:pageBlockButtons >
                <apex:commandButton value="Delete" action="{!Remove}" />
            </apex:pageBlockButtons>
              <apex:pageBlockTable value="{!courseRecords}" var="c">
                  <apex:column > 
                     <apex:inputCheckbox value="{!c.isselected}" id="InputckId"/>
                  </apex:column>
                   <apex:column > 
                     {!c.cse.Name}
                  </apex:column>
                  <apex:column > 
                     {!c.cse.course_fee__c}
                  </apex:column>
              </apex:pageBlockTable>
          </apex:pageBlock>  
  </apex:form>
</apex:page>


public with sharing class DeleteRecords_wrapperClass {

    public String SObjects { get; set; }
    public Boolean Hide { get; set; }
    Public List<wrap> Wraplist {get; set;}
    
    public DeleteRecords_wrapperClass(){
        hide= false; 
    }
   
    public List<SelectOption> getSelectObjects() {
    
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','None'));
        options.add(new SelectOption('Course','Course'));
        options.add(new SelectOption('Student','Student'));
        return options;
        
    }
    
    public Void DisplayRecords() {
         Hide= True;
         system.debug('##SObjects '+SObjects);
         if(SObjects=='Course'){
         for(Course__c c3:[select id, Name,course_fee__c from Course__c]){
             system.debug('##c3 '+c3);    
             Wraplist.add(new wrap(c3));
             system.debug('##Wraplist '+Wraplist);
         
         }
     }
}
    
  
  //wrap w; 
  Public list<Course__c> selected {get; set;}

  public List<wrap> getcourseRecords(){
     
        Wraplist = new List<wrap>();
        for(Course__c c1:[select id, Name,course_fee__c from Course__c]){
          //  w = new Wrap(c1);
           // W.isselected = false;
           // w.cse=c1;
           Wraplist.add(new wrap(c1));
        }
    // Wraplist.add(w);
     Return Wraplist;
  }

  Public void Remove(){
         selected = new list<Course__c>();
        for(wrap c2:Wraplist){
        if(c2.isselected==true){
        selected.add(c2.cse);
        }
        
        }
        Delete selected;
  }
        

    Public class wrap{
    
        public Course__c cse {get; set;}
        public boolean isselected {get; set;}
        
    public wrap(course__c cc){
    
  
    
       cse=cc;
       isselected=False;
    
       }
        
        }
}