0

Does anyone know how I can include Case History on a custom VF page on the Case object? I've tried using the tag, but that does not work. I've also tried other solutions using controllers and case.histories fields, but I keep getting a error that oldValue and newValue are not valid fields. Can anyone help?

Tried in VF Page:

<apex:pageBlock title="Case History">
    <apex:pageBlockTable value="{!case.histories}" var="c">
        <apex:column headerValue="Date"  value="{!c.createdDate}"/>
        <apex:column headerValue="From" value="{!c.oldValue}"/> 
        <apex:column headerValue="to" value="{!c.newValue}"/>
    </apex:pageBlockTable>
</apex:pageBlock>

Tried Controller:

private final Case chx;

    public list<cHistories> getHistories() {

     list<cHistories> list_ch = new list<cHistories>();

     for (Case ch: [SELECT ParentId, OldValue, NewValue, IsDeleted, Id, Field, CreatedDate, CreatedById, CreatedBy.Name
                    FROM Case
                    WHERE ParentId = :c.id
                    ORDER BY CreatedDate DESC]) {

   cHistories ch1 = new cHistories();

       ch1.theDate = String.valueOf(ch.createddate);

       ch1.who = ch.createdby.name;

       if (String.valueOf(ch.Field) == 'created') {

        ch1.action = 'Created.';

       } else if (ch.OldValue != null && ch.NewValue == null){        
        try {
         ch1.action = 'Deleted ' + Date.valueOf(ch.OldValue).format() + ' in <b>' + String.valueOf(ch.Field) + '</b>.';
        } catch (Exception e){
         ch1.action = 'Deleted ' + String.valueOf(ch.OldValue) + ' in <b>' + String.valueOf(ch.Field) + '</b>.';
        }

       } else {

        String fromText = '';
        if (ch.OldValue != null) {
         try {
          fromText = ' From ' + Date.valueOf(ch.OldValue).format();
         } catch (Exception e) {
          fromText = ' From ' + String.valueOf(ch.OldValue);
         }
        }

        String toText = '';
        try {
         toText = ' To ' + Date.valueOf(ch.NewValue).format();
        } catch (Exception e) {
         toText = ' To ' + String.valueOf(ch.NewValue);
        }

        ch1.action = 'Changed <b>' + String.valueOf(ch.Field) + '</b>' + fromText + ' to <b>' + toText + '</b>.';

       }
      list_ch.add(ch1);    
  }
  return list_ch;
    }
    public class cHistories {
        public String theDate {get; set;}
        public String who {get; set;}
        public String action {get; set;}
    }
2
  • Please edit your post to include specific examples of what you have tried. – Adrian Larson Jul 18 '16 at 16:41
  • @Adrian Larson -- OK. I edited my original post. Thanks. – J. Neilan Jul 18 '16 at 16:49
1

I have already given answer on yesterday in this post. Please refer this link:

How to add field history and product as a related list of opportunity in vf page

That post is applicable for showing Account, Opportunity, Case etc. (Standard objects) and other custom object history.

1
  • P.S. if this solves your problem then close this question – Santanu Boral Jul 19 '16 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.