0

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;
 } }
2

1 Answer 1

2

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.

1
  • Wow.. that worked. Can't believe i spent hours trying to mess with the code.
    – SfdcDev14
    Dec 22, 2015 at 19:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .