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
sumit dsumit d 

need to measure how long time between when the Case was opened and when it was first responded.

hi all,

     I need to create a way to capture when the first response. I then need to measure how long time between when the Case was opened and       when it was first responded. 

I created two fields: 
Time_to_First_Response__c 
First_Response__c

I need to capture the first response and measure the time to first response in a trigger . 

First Response is defined as when the case owner user makes any edit to the case or logs an activity against the case. 

Time to First Response Calculation: 
I need to NOT include when office is closed (nights, weekends, holidays) in calculation. 
public class CaseUpdateResponseTriggerHelper {
    public static List<Case> newCases = new List<Case>();
    public static List<Case> oldCases = new List<Case>();
    public static Map<Id, Case> newMapCases = new Map<Id, Case>();
    public static Map<Id, Case> oldMapCases = new Map<Id, Case>();
    
    public static void updateFirstResponse(){
        
        List<Case> Cases = [SELECT id, First_Response__c, Time_to_First_Response__c FROM Case];
         datetime firstresponce;
    for(Case c: newCases){     
         if(c.createddate == oldMapCases.get(c.id).lastmodifieddate && c.First_Response__c == Null ) {
            decimal miliseconds = decimal.valueOf(system.now().getTime() - c.createddate.getTime());
            Integer seconds = integer.valueof(miliseconds / 1000);
            Integer Minutes = integer.valueof(seconds / 60);
            Integer Hours   = integer.valueof(Minutes / 60);
            Integer Days    = integer.valueof(Hours / 24);
            if(seconds >= 1 && Minutes <= 0){           
                c.First_Response__c =  datetime.valueof(seconds); 
            }else if(Minutes >= 1 && Hours <= 0){           
                c.First_Response__c =  datetime.valueof(Minutes) ;
            }else if(Hours >= 1 && Days <= 0){           
                c.First_Response__c =  datetime.valueof(Hours) ;
            }else if(Days >= 1)
                c.First_Response__c =  datetime.valueof(Days) ; 

        }
    }
}
}                    how to do it with business hours?
                    any suggestions?
please provide me solution with business hours
 
IGNACIO SARMIENTO LOSADA1IGNACIO SARMIENTO LOSADA1
Hi Amit,

You could create a workflow to solve your issue. You could create a rule that only updates 'fist response' field if it is empty.
Let me know if this helps you.

Thanks