Skip to main content
3 of 9
Another example
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 CreatedDate FROM Case LIMIT 1];

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

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

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