Skip to main content
4 of 9
closed date instead of created date
martin
  • 12.7k
  • 10
  • 50
  • 75

You are going to have to do something like this first: (from your link)

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];

Then use that id as the first parameter in the method:

diff(bh.id, startdate, Datetime.now())<48)

You were getting an error message because you were using the name of the type of parameter that you needed to put into the function instead of using a value of that type.


I got this to run:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Datetime startdate = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
long l = BusinessHours.diff(bh.id,startdate,system.now());
system.debug(l);  

I this also worked for me:

BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
Case c = [SELECT ClosedDate FROM Case WHERE ClosedDate != NULL LIMIT 1];

DateTime startdate = c.ClosedDate;
DateTime systemnow = system.now();

long startl = startdate.getTime();
long nowl   = systemnow.getTime();
long diffl  = BusinessHours.diff(bh.id,startdate,systemnow);

system.debug('startl: ' + startl);
system.debug('nowl: '   + nowl);
system.debug('nowl - startl: ' + (nowl - startl));
system.debug('diffl: '  + diffl);

system.assertEquals(nowl-startl,diffl);
martin
  • 12.7k
  • 10
  • 50
  • 75