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 = 

 if (trackedFields.isEmpty()) return;

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


 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))

         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;


 if (!fieldChanges.isEmpty()) {
     insert fieldChanges;
 } }

In order to get the behavior that you're looking for where the reference field shows the name as a hyperlink to the related Account instead of just showing the id value that is stored in the field, you need to change the field type of Account__c on the AccountHistoryTracking__c object to a Lookup to the Account object.

|improve this answer|||||
  • Wow.. that worked. Can't believe i spent hours trying to mess with the code. – SfdcDev14 Dec 22 '15 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.