2

I'm trying to compare number of business hours with number of hours (bh<48) I have gone through few methods and tried the below method to get number of hours(difference between startdate and enddate :

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

But I'm constantly getting the same error for all the methods : "Method does not exist or incorrect signature: [BusinessHours].diff(Schema.SObjectField, Datetime, Datetime)"

I dont understand this error as usage of methods are referred here

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm

Any suggestions are highly appreciated! TIA

4
  • The error is expected because you are passing diff(BusinessHours.id, startdate, Datetime.now()) BusinessHours.Id is of type SObject.SObjectField. You should pass the id of instance of businesshour Sep 2, 2015 at 8:29
  • @EhsanMahmoudi I rectified that long back , I did create an instance via SOQL query and passed in diff() method. Thank you ! :)
    – SFDC Wolf
    Sep 2, 2015 at 12:49
  • What is the API version of the class that you are working on? It might be too old Sep 2, 2015 at 23:37
  • @EhsanMahmoudi, Its 34.0 its the latest one, BTW its a trigger and I got it working now ! :)
    – SFDC Wolf
    Sep 3, 2015 at 6:31

1 Answer 1

3

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);

This also works:

Trigger Test_Business_hours_Trigger on EmailMessage (after insert) {
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    list<Id> RelatedCaseIds = new list<Id>();
    for(EmailMessage em : Trigger.New){
        RelatedCaseIds.add(em.ParentId);
    }
    list<Case> CasesToUpdate = new list<Case>();
    for(Case c : [SELECT Id, ClosedDate FROM Case WHERE Id IN :RelatedCaseIds AND ClosedDate != NULL]){
        if(BusinessHours.diff(bh.Id,c.ClosedDate,system.now()) < 172800000){
            c.Status = 'Working';
            CasesToUpdate.add(c);
        }
    }
    update CasesToUpdate;
}
24
  • Thanks for the quick response Martin, I have written query already ! Still am getting the error! All the needful is done ,as per the info provided in the link.
    – SFDC Wolf
    Sep 1, 2015 at 13:15
  • @SFDCWolf The same error?
    – martin
    Sep 1, 2015 at 13:19
  • Yep! "Method does not exist" for any method mentioned in the link ! I strongly feel there is something else has to be done, I have my business hours defined in the org already !
    – SFDC Wolf
    Sep 1, 2015 at 13:22
  • @SFDCWolf it could be that you were missing the BusinessHours. before the diff() method.
    – martin
    Sep 1, 2015 at 13:32
  • @SFDCWolf Also, make sure you convert 48 from hours to milliseconds.
    – martin
    Sep 1, 2015 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.