+ Start a Discussion
Rajesh SFDCRajesh SFDC 

how to get last modified field values from case history object

here my requirments i want to get whose last modified fileds values to update to case object
using apex trigger
here my code given below:
trigger TrackigHistory on Case (before update)

    set<Id> setId = new set<Id>();
    string strhistory;
     for(Case con: trigger.new)
               List<CaseHistory> ch = [Select id,CaseId, Field, NewValue, OldValue from CaseHistory where CaseId = :setId  ];
         system.debug('&&&&&&&CaseHistory size&&&&&&&&&&'+ch.size());
       // for(integer i=0;i<ch.size();i++)
       for(CaseHistory c: ch)
          // if(ch[i].Field != NULL && ch[i].OldValue != NULL && ch[i].NewValue != NULL)
                 system.debug('&&&&&&&CaseHistory&&&&&&&&& '+'Field '+c.Field +' Changed from ' +c.OldValue+' to ' +c.NewValue);
                 con.Track_status_history__c = 'Field '+c.Field +' Changed from ' +c.OldValue+' to ' +c.NewValue;
                 how to get fields values last modified field values updat to case object


Since you want the last modified field values for the case, why not try out using context variables like oldmap, newmap etc.., to fetch the old field values? 

History objects will track the fields which are set for field history tracking and not all fields are trackable, whereas using context varaibles you can fetch the details for almost all the fields.

Will this work for you?
Anil KamisettyAnil Kamisetty

Last Modified Values are available in the After Update event not in the Before Update. The record is not yet updated, hence you won't get the Last Modified values. Try to address the scenario in different way and you will be all set.

Note : If this answers your question, please mark this as Answered, thus helping others.
Rajesh SFDCRajesh SFDC
hi ramu, in my requriments is i want to update only for last modified field values old and new fields values, i want to fetch the fielda values from case history object is it possiable or else fetch from case object ,, help me .. and also after update doesnot working its showing like a readonly record