function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumit dsumit d 

generic method to track field history

Hi All,
      i want to Develop a generic method which can be used in any object’s trigger and it should then start creating History Tracking records.
    how to do it?
Any suggestions?
bretondevbretondev
The XXXHistory tables do not support insert operation :
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_accounthistory.htm

That means if you do something like that, you'll get a "field not writeable"  error :
AccountHistory ah = new AccountHistory();
ah.NewValue = 'X';
ah.OldValue = 'Y';
//other properties

insert ah;

You can activate History Traking from the UI :
https://help.salesforce.com/articleView?id=tracking_field_history.htm&type=5
 
sumit dsumit d
hi ,
 i want to track history by generic method and i think it possible but i dont know how to do it ?
Any suggestion?
bretondevbretondev
You could do something like this :
 
public List<Object> getHistoryForRecord (String objectName, String recordId) {
     String historyObjectName = objectName + 'History';
      String idFieldName = objectName + 'Id';

    String query = 'SELECT NewValue,OldValue,Field'
             + ' FROM '
             + historyObjectName
             + ' WHERE ' 
             + idFieldName
             + '=:recordId';


     List<Object> lst = Database.query(query);
}
Here let's say you have an Account with Id = 1234 , if youd do getHistoryForRecord('Account', 1234), the method would return full history for the record.
 
sumit dsumit d
hi bretondev,
  i have a requirement in which i want to make  a generic method which can be used in any object’s trigger and it should then start creating History Tracking records. history of fields should be recorded. for this i made a visualforce page in which i have a list of sObject dropdown list and i select a Object from this list than its field are showing as checkbox than i select field which i want to track the history than these fields are saving in a object Called FieldTracking__c which has selectedField__c (textArea). this is done by me .
Now i want to develop a generic method in which i  want to track the history of field which i selected in checkboxes for particular Object.a generic method which can be used in any object’s trigger and it should then start creating History Tracking records.
a method which i call in an Object and its strat creating history for the records
how to do it ?
guide me please
i have found a link similar to it :-https://salesforce.stackexchange.com/questions/14728/how-should-i-build-custom-history-tracking-for-a-large-number-of-fields
but i didn't get it how to use this in my requirement ?
 i have a object Custom Object : HistoryTracking__c to save history tracking records for fields where history tracking is enabled.the custom Object has following fields:-Custom Fields : ObjectName__c, FieldName__c,OldValue__c,NewValue__c
how to do  it?