You need to sign in to do that
Don't have an account?
Anshuma 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?
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:
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:
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:
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
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/)