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 

want a trigger to capture when the first response

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. 
 
Naveen PoojaryNaveen Poojary
trigger updatefirstresponce on Case (before update) {
    datetime firstresponce;
    for(Case c: trigger.new){     
         if(c.createddate == trigger.oldmap.get(c.id).lastmodifieddate) {
            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_Responce__c =  string.valueof(seconds) + 'Seconds'; 
            }else if(Minutes >= 1 && Hours <= 0){           
                c.First_Responce__c =  string.valueof(Minutes) + 'Minutes';
            }else if(Hours >= 1 && Days <= 0){           
                c.First_Responce__c =  string.valueof(Hours) + 'Hours';
            }else if(Days >= 1)
                c.First_Responce__c =  string.valueof(Days) + 'Days'; 

        }
    }
    
}

Hi Amit,
The above code may help your problem.
Please make it as the best answer if it solves your problem
sumit dsumit d
First_Responce__c is a (Date/Time) field here and does it fulfil this condition also (I need to NOT include when office is closed (nights, weekends, holidays) in calculation),
any suggestion to improve more?
Regards