Pages

Friday, February 1, 2013

Accessing History of a custom or standard object


Tracking History of Salesforce Objects.....

Hi Friends,
Tracking down history of fields in salesforce custom objects is very simple and wellknown fact to most of us. 
If you dontknow it here is the procedure:
Go to: Setup-->App Setup-->Create-->object--> choose your custom object--> Edit
now Scrole down to see Optional features now enable or check Track Field History check box and save it. Now Once the track history is enabled you can see a new button showing up in  Custom Fields & Relationships  section of the Custom object page.
now all you have to do is hit the button and set history tracking to the fields.
All this is a known thing to all salesforce developers and admins.

My reason for this post is explain what's happening on checking  Track Field History checkbox on the object level. Can we control this object and modifications made on the object's record level.
Here is the explanation: 
whats happening is As soon as we check the check box and hit save on the object level.
Salesforce is creating an object(child object to the current object) which is not visible on 
Setup-->App Setup-->Create-->object page. This is just a read only object making users to read the records of the object. when ever there is a change to the track history field a record is created to thisobject with filed name old value new value parentId and user who chenged it. That is waht we are seeing in the related list of the object as chenges made on the record.
every thing is fine now the question is to why do we have to worry if about this object now. Consider a secinario where you have to take an action onthe record o lockthe record after certain number of changes have been done to the record irrespective to the number of changes on pirticular field or fields.  At this point i wanted to Querie the Hidden object to find the number of records created for each object on every change. 
in order to Query the number of records createdd to the new object we have to see it and Querie it and we salesforce gives permissions to see it thats it now we have to find the name of the object to querie it. 
Salesforce have a naming convention to this every time a new History object is created salesforce names it as custom_object__History. insted of __C for custom things it appends the object as __History.  try this use this in quering in your triggers and apex classes and make coding more comfortable.

Main points:  
  • All history Objects created by salesforce readonly and hidden
  • you can access this object by using Custom_Object__History append by__H insted of__C 

Hope this Post helps you.
You are free to Comment or ask questions on this post.


Happy Coding.

1 comment: