You need to sign in to do that
Don't have an account?
dkraun
How to add Histories relatedList to a custom visualforce page
Hi,
I need to add the field history from a custom object to a page, but am running into a lot of problems. The related list displays with no problem when viewing the custom object through the salesforce custom object tab, but on my custom page it says that Histories is not a valid child relationship name for the entity. I have checked the child relationships of my custom object and the History object that salesforce generates automatically when "Track Field History" is checked is listed with the name "Histories" as the child relationship name. I have tried this on multiple custom objects (making them as simple as possible) and a relatedList on Histories just doesn't seem to be possible. Has anyone been able to make this work?
-David
I need to add the field history from a custom object to a page, but am running into a lot of problems. The related list displays with no problem when viewing the custom object through the salesforce custom object tab, but on my custom page it says that Histories is not a valid child relationship name for the entity. I have checked the child relationships of my custom object and the History object that salesforce generates automatically when "Track Field History" is checked is listed with the name "Histories" as the child relationship name. I have tried this on multiple custom objects (making them as simple as possible) and a relatedList on Histories just doesn't seem to be possible. Has anyone been able to make this work?
-David
You can look at the WSDL to get this name, use Eclipse to see the schema, or use Apex Explorer.
My object name is CRM_Tickets, and I access this through the api name CRM_Tickets__c. I have now tried:
<apex:relatedList subject="{!ticket.Id}" list="CRM_Tickets__History"/>
but that also gives me the error of 'CRM_Tickets__History' is not a valid child relationship name for entity CRM_Ticket.
I have used the apex explorer to check on the child relationship name and I found:
Tickets__c >> Child Relationships >> Histories >> Child Relationship: Tickets__History, Relationship Name: Histories
This is why I was trying to use Histories as the list attribute in my original post. Any ideas?
Tickets__c >> Child Relationships >> Histories >> Child Relationship: Tickets__History, Relationship Name: Histories
That works! :smileyvery-happy:
Thanks
Jeroen
<apex:pageBlockTable value="{!Service__c.Histories}" var="h">
<apex:column headerValue="Date" value="{!h.createddate}"/>
<apex:column headerValue="What" value="{!h.field}"/>
<apex:column headerValue="From" value="{!h.oldvalue}"/>
<apex:column headerValue="To" value="{!h.newvalue}"/>
</apex:pageBlockTable>
</apex:pageBlock>
I too had the same error message for "Field". Using Ron's pageBlock technique and using the WSDL, I want the "Field" element to be displayed. The createddate and CreatedById by themselves display correctly in the VF page. The "Field" element causes the error messages.
<element name="Field" nillable="true" minOccurs="0" type="xsd:string" />
The normal View page displays the following after I created the record then made a change to the "Status Code" field.
Created.
Message Edited by Brian Conlon on 09-18-2008 07:40 PM
I got a nice Field History list by adding a ControllerExtension onto my VF page. I used a wrapper class to return the same 3 fields (date, user and action) that appear in other history lists. I did a bit of fanciness to provide a different action for a few different scenarios.
It's important that the VF page outputtext attribute for the action property has "escape=false". This will allow the bold html to render.
My VF markup says...
I figured out a simple way to add field history as a tab, not optimum as you need to click the link to view it, but NO need for custom controller, or extension:
<apex:tab label="Field History" name="Field History" id="fieldhistory" rendered="{!$Profile.Id!='00e0xxxxxxxx" >
<apex:outputLink value="https://ssl.salesforce.com/_ui/common/history/ui/EntityHistoryFilterPage?id={!opportunity.id}">
click to view field history
</apex:outputLink>
</apex:tab>
Interesting. I found this solution, and applied it to CaseComments. It worked, but only when I removed all but a couple fields:
Not sure what the specifics are about this, but I have my desired result.
I used Hem's solution and it works great! I am so glad you posted the code. I made a few changes for our Org but sure saved me a lot of time!
Thanks
EvaD
It would seem that the Changed Field has some some special validation that occurs when used directly as the value of an <apex:column>. I was able to get this markup to work with my Trade__c custom object:
Cheers,
-Pauly
Thanks hemm & Pauly ! your solutions worked for me !
I modified hemm's code to take approval Process fields values (locked/unlocked), now sharing it to others reference.
_____________________________________________________________________________
//___________________________ VF Code ___________________________________
<apex:pageBlockTable value="{!histories}" var="h">
<apex:column headerValue="Date" value="{!h.thedate}"/>
<apex:column headerValue="User" value="{!h.who}"/>
<apex:column headerValue="Action"><apex:outputText escape="false" value="{!h.action}"/></apex:column>
` </apex:pageBlockTable>
//____________________________________________ APEX CODE ___________________
// Wrapper class to show history in VF
public class cHistories {
// Class properties
public String theDate {get; set;}
public String who {get; set;}
public String action {get; set;}
}
public list<cHistories> getHistories() {
// Initialize list to be returned
list<cHistories> list_ch = new list<cHistories>();
// Loop through all field history records
for (LN_PST_Library__History fh: [select ParentId, OldValue, NewValue, IsDeleted, Id, Field, CreatedDate, CreatedById, CreatedBy.Name From LN_PST_Library__History where ParentId = :tempPstObj.id order by CreatedDate desc]) {
// Create a new wrapper object
cHistories ch = new cHistories();
// Set the Date
ch.theDate = String.valueOf(fh.createddate);
// Set who performed the action
ch.who = fh.createdby.name;
// Set the Action value
if (String.valueOf(fh.Field) == 'created') { // on Creation
ch.action = 'Created.';
} else if (fh.OldValue != null && fh.NewValue == null){ // when deleting a value from a field
// Format the Date and if there's an error, catch it and re
try {
ch.action = 'Deleted ' + Date.valueOf(fh.OldValue).format() + ' in <b>' + String.valueOf(fh.Field) + '</b>.';
} catch (Exception e){
ch.action = 'Deleted ' + String.valueOf(fh.OldValue) + ' in <b>' + String.valueOf(fh.Field) + '</b>.';
}
} else { // all other scenarios
if(String.valueOf(fh.Field) == 'locked'){
ch.action = 'Record <b>' + String.valueOf(fh.Field) + '</b>.';
}
else if(String.valueOf(fh.Field) == 'unlocked'){
ch.action = 'Record <b>' + String.valueOf(fh.Field) + '</b>.';
}
else{
String fromText = '';
if (fh.OldValue != null) {
try {
fromText = ' from ' + Date.valueOf(fh.OldValue).format();
}catch (Exception e) {
fromText = ' from ' + String.valueOf(fh.OldValue);
}
}
String toText = '';
try {
toText = '' + Date.valueOf(fh.NewValue).format();
} catch (Exception e) {
toText = '' + String.valueOf(fh.NewValue);
}
ch.action = 'Changed <b>' + String.valueOf(fh.Field) + '</b>' + fromText + ' to <b>' + toText + '</b>.';
}
}
list_ch.add(ch);
}
return list_ch;
}
_____________________________________________________________________________
Njoy & Happy Coding !
~ Sushil
Can you please tell me how to write test class for the above code.