+ Start a Discussion
Winston HunterWinston Hunter 

Make object inside Account that is (almost) identical to Account History

Hello.

So we want a section in the Account that looks EXACTLY like the Account History section, except it will only show for the MRR field and the section will be called MRR History.

Now, I am at the very very very beginning of learning apex/visualforce/salesforce development. I consider myself an intermediate but not-quite-yet advanced salesforce admin.  I know workflows/validation rules/process builder etc.

Is this something that I can just "learn".  The only "coding" I know is from matlab.  I am not familiar with java.  

Let me know if you think this is something that is just too advanced for where I am.
Gaurav Jain 7Gaurav Jain 7
Hi Winston,


Create custom object AccountHistoryTracking with the following field ( account__c, name__c,apiName__c,ChangedBy__c, .OldValue__c,NewValue__c, createddate__c ) and field set in account object (add those field that you want to track Eg: MRR) and Try below code:

Mark it as Best Answer, if it helps​​
trigger AccountHistoryTracker on Account (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));

                    AccountHistoryTracking__c aht = new AccountHistoryTracking__c();
                    aht.account__c = aNew.Id;
                    aht.name__c         = fieldLabel;
                    aht.apiName__c   = fieldName;
                    aht.ChangedBy__c = UserInfo.getUserId();
                    aht.OldValue__c  = oldValue;
                    aht.NewValue__c  = newValue;
                    aht.createddate__c = system.today();

                    apiNameList.add(aht.apiName__c);
                    fieldChanges.add(aht);
                }        
            }
        }
    }
    if (!fieldChanges.isEmpty()) {
        insert fieldChanges;
    }
}