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
Anshuma YadavAnshuma Yadav 

How to store updated Account object record value in another History__c (custom object) using apex class & trigger?

If the value is updated in the account object record(updation in field of the record, change in ownership of the record, added child to that record, deletion of that record), then I want to fetch the history of that record and want to store every updated record in another custom History__c object. how do we can achieve this?
PriyaPriya (Salesforce Developers) 
Hi Anshuma,

As you need to create records in custom object based on Account record changes, lets consider custom object AccountHistory__c with corresponding fields Account__c, NewName__c, OldName__c and other such fields. As you need to create different object, you should use after trigger. Below will be sample trigger:
 
trigger AccountTrigger on Account (before update, after update) {

    List<AccountHistory__c> accHisList = new List<AccountHistory__c>();

    if(Trigger.operationType==System.TriggerOperation.AFTER_UPDATE) { // After trigger context
        Map<Id, Account> oldValues = Trigger.oldMap;
        for(Account acc : Trigger.new){
            accHisList.add(new AccountHistory__c(
                Account__c = acc.Id, 
                NewName__c = acc.Name,
                OldName__c = oldValues.get(acc.Id).Name
            ));
        }
    }

    if(!accHisList.isEmpty())
        insert accHisList;
}


You do not need new custom object to track the record changes. For many Standard and all Custom Objects, you can track records history. If its standard object then you should check if Field History Tracking is available. (https://help.salesforce.com/articleView?id=tracking_field_history.htm&type=5)

First, you need to check how to Track Field History for Standard Objects (https://help.salesforce.com/s/articleView?language=en_US&id=tracking_field_history_for_standard_objects.htm&type=5) and Track Field History for Custom Objects. (https://help.salesforce.com/s/articleView?id=sf.tracking_field_history_for_custom_objects.htm&type=5)

If its standard object, you can get the history from ObjectHistory object. For example, for Account you can use below SOQL for history:

 

SELECT Id, AccountId, Field, OldValue, NewValue FROM AccountHistory

Use AccountId filter for checking the record changes for particular record.
And for custom object you will have CustomObject__History object. Below is the exmaple SOQL:
 
SELECT Id, ParentId, Field, OldValue, NewValue FROM CustomObject__History

Here, ParentId can be filtered if you need to check record changes in a particular record.
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan


 
Loulouka FormulaLoulouka Formula
Hi Anshuma,
It's possible in a before trigger, but not recommended. The benefit of using a before trigger is that you're able to make updates to the records being triggered (i.e. the instances of the records in trigger.
So more details to contact on my site Loulouka Formula (https://louloukaformula.com/)