0

I have a requirement where i need to calculate the business hours between Case closedDate and case CreatedDate without getting saturdays and sundays . Can anyone help me with the formuale how to achieve

m.dinesh
  • 1
  • 3

1 Answers1

0

Do you actually use the Calendar object, Holidays, BusinessHours model in SF setup?

https://help.salesforce.com/s/articleView?id=sf.customize_supporthours.htm&type=5 There's whole nice Apex class and diff method out of the box: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_businesshours.htm

If that's too much you could look into formula fields (https://developer.salesforce.com/docs/atlas.en-us.formula_date_time_tipsheet.meta/formula_date_time_tipsheet/formula_examples_dates.htm, "Find the Number of Weekdays Between Two Dates")


Assuming you do actually use apex and have called BusinessHours.diff...

Long differenceInMiliseconds = 12340000; // from BusinessHours.diff()
String ret = DateTime.newInstance(differenceInMiliseconds).formatGmt('hh:mm:ss');
System.debug(ret); // 03:25:40
// 3 * 3600 + 25 * 60 + 40 = 10800 + 1500 + 40 = 12340, cool
eyescream
  • 18,088
  • 2
  • 34
  • 46
  • I am getting Number Of Hours between the Case ClosedDate and CreatedDate But I need them in the below format as HH:MM:SS – m.dinesh Oct 26 '21 at 14:23
  • post what you have so far, what have you tried. We aren't a free coding service. You tagged it apex but I suspect you don't have actual code, just formula fields? If you do code - it's not rocket science to divide by 60 few times or use DateTime.format – eyescream Oct 26 '21 at 17:27
  • I am getting Only Business Days. From This I need to Calculate Business Hours ROUND( 8 * ( ( 5 * FLOOR( ( DATEVALUE( ClosedDate ) - DATE( 1900, 1, 8) ) / 7) + MIN(5, MOD( DATEVALUE( ClosedDate ) - DATE( 1900, 1, 8), 7) + MIN( 1, 24 / 8 * ( MOD( ClosedDate - DATETIMEVALUE( '1900-01-08 16:00:00' ), 1 ) ) ) ) ) - ( 5 * FLOOR( ( DATEVALUE( CreatedDate ) - DATE( 1900, 1, 8) ) / 7) + MIN( 5, MOD( DATEVALUE( CreatedDate ) - DATE( 1996, 1, 1), 7 ) + MIN( 1, 24 / 8 * ( MOD( CreatedDate - DATETIMEVALUE( '1900-01-08 16:00:00' ), 1) ) ) ) ) ), 0 ) – m.dinesh Oct 29 '21 at 14:08
  • Sorry, I'm too sober for this formula. You could try posting on https://salesforce.stackexchange.com, better chance of it being seen by admins and other low-code users. You tagged it with apex programming language so I gave you a built-in that can calculate it with few lines of code - but achieving same thing in formula fields might be tricky. – eyescream Oct 29 '21 at 14:55