I am using this trigger i found on this forum to track custom fields in my Account object. I am trying to figure out how to make the Account__c field to be a clickable Account Name field. Right now it just shows a ID string.
I created a set to store all IDs from the trigger.new and then tried to query for the name in the Account__C field and was getting invalid sobject type error. Here is the trigger. THanks
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();
System.debug( fieldname + fieldLabel );
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.Account__c = newAccount.id;
accountHistory.ChangedBy__c = UserInfo.getUsername();
accountHistory.OldValue__c = oldValue;
accountHistory.NewValue__c = newValue;
fieldChanges.add(accountHistory);
}
}
if (!fieldChanges.isEmpty()) {
insert fieldChanges;
} }