Below are my scenarios need to achieve in an anonymous script.

I have created 5 fields (ClosedDate__c, NewStatusDate__c, EscalateDate__c, InProgressDate and OnHoldDate__c). In my instance, I have more 2000 records. I want to just track when each status has been the change for those 2000 records.

Say for the example:

I have record changed New to In Progress 4 weeks back. After 2 weeks, it has been changed to On hold.Today I changed the status to Closed. There will be History tracking in Case detail page. I want to populate those date to appropriate fields based on status history.

Here I did some workarounds:

 List<Case> caseList = new List<Case>();
    List<CaseHistory> CaseHistoryList = new List<CaseHistory>();
    Map<Id, Date> caseIdMap  = new Map<Id, Date>();
    //Querying Case Records here
    for(Case cs: [SELECT Id, Status FROM Case]){
    if(caseList.size() > 0){
       //Querying history for Case
       CaseHistoryList = [SELECT newValue, createdDate, OldValue, CaseId FROM CaseHistory WHERE CaseID IN :caseList AND Field = 'Status'];
        System.debug('CaseHistoryList: '+CaseHistoryList);
        if(CaseHistoryList.size() > 0){
            for(CaseHistory csHis : CaseHistoryList){
                if(csHis.newValue == 'New'){
                    caseIdMap.put(csHis.caseId, csHis.CreatedDate.date());
                    System.debug('CaseHistory Map: '+caseIdMap);
        if(caseIdMap.size() > 0){
            for(Case updateCase : [SELECT Id, ClosedDate__c, NewStatusDate__c, EscalateDate__c, InProgressDate, OnHoldDate__c Status FROM Case WHERE ID in :caseIdMap.keyset()]){
               //Need to populate date field for each status.

You can cut this down quite a bit, that is if I understand your requirements correctly, just use Maps.. I recommend putting a limiter on this, 2000 is a lot of records, meaning you should be passing something into the first for query to only select certain cases.. I just reread this and you want to do this for an execute anon script and I don't know thing that is the route you should go or at least do this in a QA environment first. I would then create batch apex to handle this in the future

Map<ID, Case> caseMap = new Map<ID, Case>();

for(Case cs: [SELECT Id, Status FROM Case])
    caseMap.put(cs.ID, cs);

if(caseMap.size() > 0)
    for(CaseHistory caseHistory :  [SELECT newValue, createdDate, OldValue, CaseId FROM CaseHistory WHERE CaseID IN : caseMap.keySet() AND Field = 'Status'])
        if(caseHistory.newValue == 'New' && caseMap.containsKey(caseHistory.CaseID))
            /*caseMap.get(caseHistory.CaseID).ClosedDate__c = caseHistory.???
            caseMap.get(caseHistory.CaseID).NewStatusDate__c = .

  update caseMap.values();
  • Hey @EricSSH, Thanks for your reply. All workarounds are going on Dev Sandbox only. There is three more sandboxes environment to test this requirement before moving to production. Seems like it will work. I will test this and let you know. Again, Thanks for your help. – RajDevBsc Apr 23 '18 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.