Skip to main content
Fix horrible formatting. Invert some statements to aide readability.
Source Link
trigger AccountHistoryTracker on Account (after update) {
    
    final List<Schema.FieldSetMember> trackedFields = 
        SObjectType.Account.FieldSets.HistoryTracking.getFields();
    
    if (trackedFields.isEmpty()) return;
 
    final List<AccountHistoryTracking__c> fieldChanges = 
        new List<AccountHistoryTracking__c>();

 List<string> apiNameList = new List<string>if(!trigger.isUpdate); 
        return;

if(Trigger.isUpdate){
    for (Account aNewnewAccount : trigger.new) {

        final Account aOldoldAccount = trigger.oldmap.get(aNewnewAccount.Id);

        for (Schema.FieldSetMember fsm : trackedFields) {

            String fieldName  = fsm.getFieldPath();
            String fieldLabel = fsm.getLabel();

            if (aNewnewAccount.get(fieldName) !=== aOldoldAccount.get(fieldName)) 
 {               continue;

            String oldValue = String.valueOf(aOldoldAccount.get(fieldName));
            String newValue = String.valueOf(aNewnewAccount.get(fieldName));
            
            if (oldValue != null && oldValue.length()>255) 
                oldValue = oldValue.substring(0,255);
     
            if (newValue != null && newValue.length()>255) 
                newValue = newValue.substring(0,255); 

            final AccountHistoryTracking__c ahtaccountHistory = 
                new AccountHistoryTracking__c();
aht
            accountHistory.name         = fieldLabel;
aht            accountHistory.apiName__c   = fieldName;
aht            accountHistory.User__c      = aNewnewAccount.Id;
aht            accountHistory.ChangedBy__c = UserInfo.getUserId();
aht            accountHistory.OldValue__c  = oldValue;
 aht           accountHistory.NewValue__c  = newValue;

 apiNameList.add(aht.apiName__c);
           fieldChanges.add(ahtaccountHistory);
}        
 }
    }
}    
    if (!fieldChanges.isEmpty()) {
        insert fieldChanges;
    }
 }
trigger AccountHistoryTracker on Account (after update) {
List<Schema.FieldSetMember> trackedFields =     SObjectType.Account.FieldSets.HistoryTracking.getFields();
if (trackedFields.isEmpty()) return;

 List<AccountHistoryTracking__c> fieldChanges = new List<AccountHistoryTracking__c>();

 List<string> apiNameList = new List<string>();        

if(Trigger.isUpdate){
for (Account aNew : trigger.new) {

Account aOld = trigger.oldmap.get(aNew.Id);

for (Schema.FieldSetMember fsm : trackedFields) {

 String fieldName  = fsm.getFieldPath();
String fieldLabel = fsm.getLabel();

if (aNew.get(fieldName) != aOld.get(fieldName)) {

String oldValue = String.valueOf(aOld.get(fieldName));
String newValue = String.valueOf(aNew.get(fieldName));
if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255);
 if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255); 

 AccountHistoryTracking__c aht = new AccountHistoryTracking__c();
aht.name         = fieldLabel;
aht.apiName__c   = fieldName;
aht.User__c      = aNew.Id;
aht.ChangedBy__c = UserInfo.getUserId();
aht.OldValue__c  = oldValue;
 aht.NewValue__c  = newValue;

 apiNameList.add(aht.apiName__c);
 fieldChanges.add(aht);
}        
 }
}
}
if (!fieldChanges.isEmpty()) {
 insert fieldChanges;
 }
 }
trigger AccountHistoryTracker on Account (after update) {
    
    final List<Schema.FieldSetMember> trackedFields = 
        SObjectType.Account.FieldSets.HistoryTracking.getFields();
    
    if (trackedFields.isEmpty()) return;
 
    final List<AccountHistoryTracking__c> fieldChanges = 
        new List<AccountHistoryTracking__c>();

    if(!trigger.isUpdate) 
        return;

    for (Account newAccount : trigger.new) {

        final Account oldAccount = trigger.oldmap.get(newAccount.Id);

        for (Schema.FieldSetMember fsm : trackedFields) {

            String fieldName  = fsm.getFieldPath();
            String fieldLabel = fsm.getLabel();

            if (newAccount.get(fieldName) == oldAccount.get(fieldName)) 
                continue;

            String oldValue = String.valueOf(oldAccount.get(fieldName));
            String newValue = String.valueOf(newAccount.get(fieldName));
            
            if (oldValue != null && oldValue.length()>255) 
                oldValue = oldValue.substring(0,255);
     
            if (newValue != null && newValue.length()>255) 
                newValue = newValue.substring(0,255); 

            final AccountHistoryTracking__c accountHistory = 
                new AccountHistoryTracking__c();

            accountHistory.name         = fieldLabel;
            accountHistory.apiName__c   = fieldName;
            accountHistory.User__c      = newAccount.Id;
            accountHistory.ChangedBy__c = UserInfo.getUserId();
            accountHistory.OldValue__c  = oldValue;
            accountHistory.NewValue__c  = newValue;

            fieldChanges.add(accountHistory);
        }
    }
    
    if (!fieldChanges.isEmpty()) {
        insert fieldChanges;
    }
}
Source Link
sfdc
  • 13.8k
  • 19
  • 136
  • 218

Create an object called AccountHistoryTracking and fields like APIName__c,OldValue__c,NewValue__c then create a fieldset on Account called'HistoryTracking' with the fields you want to track on Account and then write below code to track history on account.This way you don't need to worry about history tracking limit per object.

trigger AccountHistoryTracker on Account (after update) {
List<Schema.FieldSetMember> trackedFields =     SObjectType.Account.FieldSets.HistoryTracking.getFields();
if (trackedFields.isEmpty()) return;

 List<AccountHistoryTracking__c> fieldChanges = new List<AccountHistoryTracking__c>();

 List<string> apiNameList = new List<string>();        

if(Trigger.isUpdate){
for (Account aNew : trigger.new) {

Account aOld = trigger.oldmap.get(aNew.Id);

for (Schema.FieldSetMember fsm : trackedFields) {

 String fieldName  = fsm.getFieldPath();
String fieldLabel = fsm.getLabel();

if (aNew.get(fieldName) != aOld.get(fieldName)) {

String oldValue = String.valueOf(aOld.get(fieldName));
String newValue = String.valueOf(aNew.get(fieldName));
if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255);
 if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255); 

 AccountHistoryTracking__c aht = new AccountHistoryTracking__c();
aht.name         = fieldLabel;
aht.apiName__c   = fieldName;
aht.User__c      = aNew.Id;
aht.ChangedBy__c = UserInfo.getUserId();
aht.OldValue__c  = oldValue;
 aht.NewValue__c  = newValue;

 apiNameList.add(aht.apiName__c);
 fieldChanges.add(aht);
}        
}
}
}
if (!fieldChanges.isEmpty()) {
 insert fieldChanges;
 }
 }