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

Account History Tracking with Apex

Salesforce has history tracking for many objects but nothing for accounts that I can tell. 
It sounds like Apex will be able to help us here but is it the best solution?  Are their other ways to do this?
Our current business problem is history tracking on the account level, there are several extremely important fields and we need to know:
-When they changed
-Who changed them
-What value was before change
-What value is after change
We could create a custom object called "Account History" that could store all the relevant info. Using APEX can we create one of these "Account History" objects each time an account is Created\Updated\Saved?
We are going through an audit and our auditors want a see a way to track... well almost everything!
Can I do it using Apex?  Or should I do it some other way?
This would certainly be something you could easily do in Apex code.

With Apex code you can create a trigger on the account object.  That trigger can be configured to fire on update, insert or delete.  In the case of tracking changes, the trigger gives you access to the object in two states, current (unchanged) and new (the changes).  This gives you the ability to record the "from" and "to" values for any fields on the Account.  You would, of course, want to implement some rules as to what kind of change warrants a history record.

In addition to insert, update and delete events, you qualify those events with regard to order, before insert or after insert for instance.  In the case of audit you would create trigger for after insert, after update and after delete.

trigger accountAudit on Account (after insert, after update, after delete) {
    if (Trigger.isInsert) {
        //add a new history item indicating the creation of the object
        //Get the user id from createdBy
        String cId =;
    } else if (Trigger.isUpdate) {
        //We only care if the name was changed
        if (Trigger.old.Name != {
            //add a history item
            String oldName = Trigger.old.Name;
            String newName =;


Fantastic, thanks Dave!  I can't wait to start playing with Apex, I don't see any beta release dates on the web site, can I assume we will get an email announcement when it is released?  Is there any time frame for release at the moment?
Q1 2007 for a beta

So I am testing this out and I am going along but I am having a problem with the lookup field.  I created a Custom object called "Account History" and added a lookup to the account.  Then added this trigger to the account object:

trigger accountAudit on Account (after insert, after update) {

if (Trigger.old.Name !=


Account_History__c acctHistory = new Account_History__c();

acctHistory.Account_Name_before__c = Trigger.old.Name;

acctHistory.Account_Name_after__c =;

insert acctHistory;




Works great but the lookup field in the resulting account history object is blank and I am not sure how to set this.  I have tried:

acctHistory.AccountID =;

acctHistory.Account__C =;

acctHistory.Account__C =;

acctHistory.Account =;

Nothing seems to work, how do I fill in the lookup field on the Account History object with the right account?

Of course as soon as I post I get it:

acctHistory.Account__C =;


any different using the new.ID or old.ID?  In theory they should be the same...



Is this ready for production or just in the as yet unreleased APEX environment?

I need to track Account owner history and trigger work flow alerts to the pervious owner when account is taken away.

Are there any other alternative solutions out there, either within the Native app that I may be missing or custom solutions floating around?

Any assistance would be greatly appreciated.


Kevin KneipKevin Kneip
Do you have the Apex Test class for this trigger?