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;
}
}
Fix horrible formatting. Invert some statements to aide readability.