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
ankethaanketha 

Need Business hours to be displayed on the case from the date it was created

Hi,

I have the following code:

public static string getBusinessHours(ID CaseId){
        string TimeDiff;
        BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
        Case c = [Select id,CreatedDate from case where id=:CaseId];
        
        Datetime CurrentDateTime = System.now();
        Datetime CreatedDateTime = c.CreatedDate ;
        //Datetime ClosedDateTime = ncs[i].ClosedDate ; 
        //Datetime ClosedDateTime =ncs[i].LastModifiedDate;
    
        Decimal diffHours = BusinessHours.diff(bh.ID,CreatedDateTime,CurrentDateTime);
Decimal days= (diffHours/86400000).round();  // converting milliseconds into days
Decimal hours = (diffHours/3600000).round();  // converting milliseconds into Hours
        Decimal minutes = (diffHours/60000).round();  // converting milliseconds into minutes
        Decimal seconds = (diffHours/1000).round();  // converting milliseconds into seconds
        
        TimeDiff = hours + ':' + minutes + ':' + seconds;
             
        return TimeDiff;
    }

This will return the days:hours:mins:seconds. But if the created date is 25/9/2018, the result is 80:1922:115331:6919867
But I want it to be displayed as 80 days and number of minutes after those many hours and seconds after those many minutes.
I don't want the total number of hours, minutes and seconds from the created date.

Can anyone please help on this?

Regards,
Anketha
Deepali KulshresthaDeepali Kulshrestha
Hi Anketha,

Try the following code it may be helpful for you:
public static string getBusinessHours(ID CaseId){
        string TimeDiff;
        BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
        Case c = [Select id,CreatedDate from case where id=:CaseId];
        
        Datetime CurrentDateTime = System.now();
        Datetime CreatedDateTime = c.CreatedDate ;
        //Datetime ClosedDateTime = ncs[i].ClosedDate ; 
        //Datetime ClosedDateTime =ncs[i].LastModifiedDate;
    
        Decimal diffHours = BusinessHours.diff(bh.ID,CreatedDateTime,CurrentDateTime);
        Decimal days= (diffHours/86400000).round();  // converting milliseconds into days
        Decimal hours = (diffHours/3600000).round();  // converting milliseconds into Hours
        Decimal minutes = (diffHours/60000).round();  // converting milliseconds into minutes
        Decimal seconds = (diffHours/1000).round();  // converting milliseconds into seconds
        
         Decimal newHours=days*24-hours;
            Decimal newminutes=((24*days*60)+newHours*60)-minutes;
            Decimal newseconds=((24*days*60*60)+(newHours*60+newminutes*60))-seconds;
        TimeDiff = days +':'+ newHours + ':' + newminutes + ':' + newseconds;
             
        return TimeDiff;
    }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
ankethaanketha
Hi Deepali,

Thanks for your response on this. 

The result of my previous code from the date 25/09/2018 12:43 PM was 81:1937:116237:6974191

And now after I add the newHours,newminutes and newseconds, the result is 81:7:823:74009. I don't think this is correct as well. because I want the minutes and seconds to be within 60.

Regards,
Anketha
Deepali KulshresthaDeepali Kulshrestha
Hi Anketha,

Try the following code it may be helpful for you:
public class getBusinessHours {
        public static string getBusinessHoursMethod(ID CaseId){
        string TimeDiff;
        BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
        Case c = [Select id,CreatedDate from case where id=:CaseId];
        
        Datetime CurrentDateTime = System.now();
        Datetime CreatedDateTime = c.CreatedDate ;
        Decimal diffHours = BusinessHours.diff(bh.ID,CreatedDateTime,CurrentDateTime);
        Decimal days= (diffHours/86400000).round();  // converting milliseconds into days
        Decimal hours = (diffHours/3600000).round();  // converting milliseconds into Hours
        Decimal minutes = (diffHours/60000).round();  // converting milliseconds into minutes
        Decimal seconds = (diffHours/1000).round();  // converting milliseconds into seconds
            Decimal newdays=days-1;
            Decimal newHours=(hours-newdays*24);
            Decimal newminutes=minutes-newHours*60;
            Decimal newseconds=seconds-minutes*60;
        TimeDiff = newdays +':'+ newHours + ':' + newminutes+':'+newseconds ;
             system.debug('TimeDiff->'+TimeDiff);
        return TimeDiff;
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha