function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajiv B 3Rajiv B 3 

Trigger to capture before and after update

Hi all, 

Need to capture  user/jobs details who updated case  in both before update (Before update record) , after update(who updated ) ?
need to store  last 30 days records modified user details and modified 10 field values future reference. 

can you please assist me with trigger code. 

 
Best Answer chosen by Rajiv B 3
Dayakar.DDayakar.D
Hi Rajiv,

Create one custom object to store information regaring field updates, and write a trigger on case as shown below.
I have done this on Account object, I am posting my code just for your reference.

trigger 
trigger HistoryTracking on Account (after Update) {
    if(trigger.isAfter){
       HistoryTrackingHelperClass.trackHistory(trigger.oldMap, trigger.NewMap);
    }
}

HelperClass:
public class HistoryTrackingHelperClass {
    static Map<string,Object> oldMapValues= new Map<string,Object>();
    static Map<string,Object> newMapValues= new Map<string,Object>();
    public static void trackHistory(Map<id,Account> oldAccounts, Map<id,Account> newAccounts)
    {
        Datetime hDate=system.now();
        list<Account> AccountsList=newAccounts.values();
        Set<History_Tracking__c> historyTrackingFields= new Set<History_Tracking__c>();
        for(string ids:newAccounts.keySet())
        {
            account oldAcc=oldAccounts.get(ids);
            account newAcc=newAccounts.get(ids);
            Map<String, Object> fieldsToValue = oldAcc.getPopulatedFieldsAsMap();
            system.debug('fieldsToValue'+fieldsToValue);
            for(string s:fieldsToValue.keySet()){
                oldMapValues.put(s, fieldsToValue.get(s));
            }
            Map<String, Object> newFieldsToValue = newAcc.getPopulatedFieldsAsMap();
            for(string s:newFieldsToValue.keySet()){
                newMapValues.put(s, newFieldsToValue.get(s));
            }
            system.debug('newFieldsToValue'+newFieldsToValue);
            for(string fieldName:newFieldsToValue.keySet())
            {
                if(oldMapValues.get(fieldName)!=newMapValues.get(fieldName) && fieldName!='LastModifiedDate' && fieldName!='SystemModstamp')
                {
                    History_Tracking__c historyObj = new History_Tracking__c();
                    historyObj.Date__c=hDate;
                    historyObj.User__c=userinfo.getUserId();
                    historyObj.account__c=(string)oldMapValues.get('Id');
                    historyObj.Action__c='Changed '+fieldName+' from '+oldMapValues.get(fieldName)+' to '+newMapValues.get(fieldName);
                    historyTrackingFields.add(historyObj);
                }
            }
            for(string fieldName:fieldsToValue.keySet())
            {
                if(oldMapValues.get(fieldName)!=newMapValues.get(fieldName) && fieldName!='LastModifiedDate' && fieldName!='SystemModstamp' )
                {
                    History_Tracking__c historyObj = new History_Tracking__c();
                    historyObj.Date__c=hDate;
                    historyObj.User__c=userinfo.getUserId();
                    historyObj.account__c=(string)oldMapValues.get('Id');
                    historyObj.Action__c='Changed '+fieldName+' from '+oldMapValues.get(fieldName)+' to '+newMapValues.get(fieldName);
                    historyTrackingFields.add(historyObj);
                }
            }
            list<History_Tracking__c>recordsList=new list<History_Tracking__c>();
            system.debug('historyObj'+historyTrackingFields);
            for(History_Tracking__c hitoryRec:historyTrackingFields)
            {
                recordsList.add(hitoryRec);
            }
            insert recordsList;
        } 
    }
}

Please let me know, if it helps you.

BestRegards,
Dayakar.D

All Answers

Dayakar.DDayakar.D
Hi Rajiv,

Create one custom object to store information regaring field updates, and write a trigger on case as shown below.
I have done this on Account object, I am posting my code just for your reference.

trigger 
trigger HistoryTracking on Account (after Update) {
    if(trigger.isAfter){
       HistoryTrackingHelperClass.trackHistory(trigger.oldMap, trigger.NewMap);
    }
}

HelperClass:
public class HistoryTrackingHelperClass {
    static Map<string,Object> oldMapValues= new Map<string,Object>();
    static Map<string,Object> newMapValues= new Map<string,Object>();
    public static void trackHistory(Map<id,Account> oldAccounts, Map<id,Account> newAccounts)
    {
        Datetime hDate=system.now();
        list<Account> AccountsList=newAccounts.values();
        Set<History_Tracking__c> historyTrackingFields= new Set<History_Tracking__c>();
        for(string ids:newAccounts.keySet())
        {
            account oldAcc=oldAccounts.get(ids);
            account newAcc=newAccounts.get(ids);
            Map<String, Object> fieldsToValue = oldAcc.getPopulatedFieldsAsMap();
            system.debug('fieldsToValue'+fieldsToValue);
            for(string s:fieldsToValue.keySet()){
                oldMapValues.put(s, fieldsToValue.get(s));
            }
            Map<String, Object> newFieldsToValue = newAcc.getPopulatedFieldsAsMap();
            for(string s:newFieldsToValue.keySet()){
                newMapValues.put(s, newFieldsToValue.get(s));
            }
            system.debug('newFieldsToValue'+newFieldsToValue);
            for(string fieldName:newFieldsToValue.keySet())
            {
                if(oldMapValues.get(fieldName)!=newMapValues.get(fieldName) && fieldName!='LastModifiedDate' && fieldName!='SystemModstamp')
                {
                    History_Tracking__c historyObj = new History_Tracking__c();
                    historyObj.Date__c=hDate;
                    historyObj.User__c=userinfo.getUserId();
                    historyObj.account__c=(string)oldMapValues.get('Id');
                    historyObj.Action__c='Changed '+fieldName+' from '+oldMapValues.get(fieldName)+' to '+newMapValues.get(fieldName);
                    historyTrackingFields.add(historyObj);
                }
            }
            for(string fieldName:fieldsToValue.keySet())
            {
                if(oldMapValues.get(fieldName)!=newMapValues.get(fieldName) && fieldName!='LastModifiedDate' && fieldName!='SystemModstamp' )
                {
                    History_Tracking__c historyObj = new History_Tracking__c();
                    historyObj.Date__c=hDate;
                    historyObj.User__c=userinfo.getUserId();
                    historyObj.account__c=(string)oldMapValues.get('Id');
                    historyObj.Action__c='Changed '+fieldName+' from '+oldMapValues.get(fieldName)+' to '+newMapValues.get(fieldName);
                    historyTrackingFields.add(historyObj);
                }
            }
            list<History_Tracking__c>recordsList=new list<History_Tracking__c>();
            system.debug('historyObj'+historyTrackingFields);
            for(History_Tracking__c hitoryRec:historyTrackingFields)
            {
                recordsList.add(hitoryRec);
            }
            insert recordsList;
        } 
    }
}

Please let me know, if it helps you.

BestRegards,
Dayakar.D
This was selected as the best answer
Rajiv B 3Rajiv B 3
Thanks Dayakar.... You have done a great help..
can you provide test class for this ??