loading... Salesforce Cat
Showing posts with label History tracking. Show all posts
Showing posts with label History tracking. Show all posts

Thursday, November 6, 2014

Salesforce History Tracking :Access Field History From Apex Code


In my last post Salesforce History Tracking - Step by Step Guide I discussed in detail about enabling history tracking on a custom object and viewing those logs using Salesforce reports and History tracking related list.In this post I am going to discuss how field tracking logs can be accessed from Apex code.

Salesforce maintains separate internal object to maintain history logs for each history tracking enabled object.The naming of these tables follows a simple convention so should be easy to find. For standard objects the name of the history table follows the format: '<ObjectName>History' so for the Account object the history table is AccountHistory.

For custom objects the name of the convention simply replaces the 'c' on the end of the API name with 'History'. So, for a custom object call My_Object__c the history table is called My_Object_History.

All history tracking tables has following fields.


  • ParentId : This is the Id  of record this history tracking log is related to (id of record field modification happend)
  • Field      :  API name of the modified field.
  • OldValue : Field value prior to the modification.
  • NewValue : Field value after modification
  • CreatedById : Id of the User who made the modification. 
History Tracking logs can be queried via SOQL as shown below

Query all field modification logs of a custom object called  My_Object__c

 Select Id,Field,CreatedById ,OldValue,NewValue,EditedBy From My_Object__History  

Query All field modification logs related to a field called My_Field__c of My_Object__c



 Select Id,Field,CreatedById ,OldValue,NewValue,EditedBy From Friend__History   
 Where Field ='My_Field__c'  


Query  All field modfication logs related to a particular record

 
 Select Id,Field,CreatedById ,OldValue,NewValue,EditedBy From Friend__History   
 Where ParentId=:RecordId   


Using SOQL to query history logs you can build your own Visualforce pages to display field modification details to the end users. 

Sunday, October 26, 2014

Salesforce History Tracking - Step by Step Guide

One of the advantages of working on a cloud platform like Salesforce is some really cool and important functions comes inbuilt and only we have to do is setting up those according to your requirements.One such feature is Salesforce History Tracking.

History tracking is the Salesforce way of monitoring and auditing the changes done to data (object records) by the users. In any business monitoring and auditing data changes is important for security and administrative purposes.In this article I am going to discuss how to enable history tracking on a Custom object and how to monitor the changes done by the users to records of history tracking enabled object.

To learn more refer Salesforce History Tracking

1. Go to the object you want to track history - here my object is Friend.




 2. Click on the Edit button and scroll down to Optional Features section and tick  following options

  • Track Field History
  • Allow Reports


3. Now on top of your Custom Field List  you can see a new button Set History Tracking. Click on it and Select the Fields on the object you want monitored. You are only allowed to track maximum of 20 fields so choose wisely :)




Now we are done with Setting up the object for history tracking. This point  forward any changes made to the  tracking enable fields of the object will be logged by Salesforce. Our next task is to create a report on those logs. so we can view those tracking details.

4. Click on the plus (+) on your tab list .


5. Scroll to Reports link and click on it.



6. Click New Report




7. Then Select the Report Type .This should be  "< your object name>  History". Since my object is Friend , reporting Type is "Friend History".

  • If reporting type is not available please check you have enabled Allow Reports option on your object
  • Reporting Type is not created for Detail objects of Master - Detail relationship so please make sure your object (History Tracking enabled) is not a detail object of a master -Detail relationship.
8. Click on Create

  

Parent ID field appearing in the history records is the Salesforce ID of the actual record to which that history record is related.


9. Customize the report according to your requirements and click on Save  

  • Give a name to your report
  • Select the Folder you want to save the report based on who you want to allow to access the report
  Now we are done.


10. Then you can run the report to see the details



11.  Optionally you can add History tracking related list to your page layout (just click related lists and drag and drop <object Name> History  list to your layout and Save the layout).Since it will show the field tracking details to the all the users who has read access to the records it is not the best idea but it depends on your Application's requirement and data sensitivity.




important points to note

  • History tracking starts from point you enable history tracking. No changes prior to enabling history tracking won't be available..
  • Changes to Fields with more than 255 characters are tracked as Edited. Their old and new values are not recorded.
  • Cannot create History report on Custom objects which has  master-Detail relationships.
  • If you disable history tracking for custom object, you cannot report on its  field history.