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;}
}