Trigger to determine when a field was last populated

After extensive research I think I know what I need to do, but I don’t know how to get there

basically, I need to create a trigger to determine when a field was last populated.

I know this can be done via workflow and using the now() function for eg, if field is changed populate with date. However I have over 50 fields per object so don’t want to great 50 new date/time fields.

Now my idea is to Create a new object called “Analysis” and have the following three fields
“Object Name”, “field” , “Last Populated ”. (Not sure whether to use api name or field name)

For eg (If this field was last populated four days ago and trigger run today)
Object Name = Account
Field Name = Agent Code
Api Name = Agent_Code__c
Last Populated = 01/06/2014

For eg (If this field was last today and trigger run today)
Object Name = Account
Field Name = Agent Code
Api Name = Agent_Code__c
Last Populated = 05/06/2014

Answer

Create an object called AccountHistoryTracking with 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:

trigger AccountHistoryTracker on Account (after insert,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);

                }        

            }

        }

        List<AccountHistoryTracking__c> ahtToDelete = [select Id,apiName__c from AccountHistoryTracking__c where apiName__c in:apiNameList];

        if(ahtToDelete.size()>0){
            delete ahtToDelete;
        }
    }

    if(Trigger.isInsert){
        for (Account aNew : trigger.new) {
            for (Schema.FieldSetMember fsm : trackedFields) {

                String fieldName  = fsm.getFieldPath();
                String fieldLabel = fsm.getLabel(); 
                String newValue = String.valueOf(aNew.get(fieldName));
                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.NewValue__c  = newValue;

                apiNameList.add(aht.apiName__c);
                fieldChanges.add(aht);

            }
        }
    }

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

Attribution
Source : Link , Question Author : user8746 , Answer Author : Programmable Medley

Leave a Comment