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
Srinivasa Rao Balle 1Srinivasa Rao Balle 1 

Case business hours calculation with apex trigger

Hi All,

I have one requiremeent to show the first touch hours on case object, these are the details.

On case object we have case open date field and first touch date fields. i am updating the first touch date when ever the case status changes from NEW to Other picklist values, from this field i am caiculating the Business TAT hours with following code.

if(trigger.isBefore && trigger.isUpdate){
        for(case caseRec: trigger.new){
            if(trigger.newMap.get(caseRec.id).Status !='New' && trigger.oldMap.get(caseRec.id).Status =='New'){
                    caseRec.First_Touch_Date__c = system.now();

//Get the default business hours (we might need it)
                   BusinessHours defaultHours = [select Id from BusinessHours where IsDefault=true];
                //On the off-chance that the business hours on the case are null, use the default ones instead
                Id hoursToUse = caseRec.BusinessHoursId!=null?caseRec.BusinessHoursId:defaultHours.Id;
 Long timeSinceLastStatus = BusinessHours.diff(hoursToUse,caseRec.createdDate, caseRec.First_Touch_Date__c)/ 1000L / 60 / 60;
                 System.debug(timeSinceLastStatus);
                caseRec.Business_TAT_hours__c = timeSinceLastStatus;
                
            }

}
}

now if the case is created at  8/9/2016 3.15 PM  and First touch date is  8/9/2016 4.53 PM then my Business Hours(TAT) should be 1 hour 38 minutes. but here it is showing only 1, what should i do to get decimal minutes value also in the above code.

Any help is appricaited. i refered some of these links

http://forcemonkey.blogspot.com/2009/11/calculating-business-hours.html