1

I currently have business hours / Holidays removed from business hour calculations using formula fields. The issue with that is, is an employe works on a holiday or weekend, the formula shows a negative number and I can not figure out a way to resolve that issue, so I am attempting to move to Apex to calculate the number of business hours and holidays. When attempting to do so via an after insert / after update trigger which calls a class to use the business hour object and the diff method I receive the following error: Method does not exist or incorrect signature: void diff(Id, DateTime, DateTime) from the type BusinessHours

Any idea what my issue is here, and how to resolve?

Trigger Code:

trigger ticketTrigger on Ticket__c (after insert, after update) {
    
    List<Ticket__c> ticketsToUpdate = new List<Ticket__c>();
    
    for(Ticket__c tickets : Trigger.New){
        ticketTriggerHandler.sla(Trigger.new); 
    }
    
}

Class Code:

public class ticketTriggerHandler {

    public static void sla(List<Ticket__c> newTickets){
        
        BusinessHours bh = [SELECT ID FROM BusinessHours WHERE IsDefault=true];
        
        list<Ticket__c> ticketsToUpdate = new List<Ticket__c>();
        
        List<Ticket__c> ticketsFromTrigger = newTickets;
        
        for(Ticket__c t : ticketsFromTrigger){
            DateTime create = t.Created_Date__c;
            DateTime Respond = t.Responded_Date__c;
            DateTime Resolve = t.Resolved_Date__c;
            Integer RespondDiff = bh.diff(bh.id, create, Respond);
            Integer ResolveDiff = bh.diff(bh.Id, create, Resolve);
            t.RespondedHours__c = RespondDiff;
            t.ResolvedHours = ResolveDiff;
            ticketsToUpdate.add(t);
        }
        if(ticketsToUpdate.size()>0){
            update ticketsToUpdate;
        }
    }
}

UPDATED CODE::::::::

public class ticketTriggerHandler {

@invocableMethod
public static void sla(List<Ticket__c> newTickets){
    List<ID> ticketIds = new List<ID>();
    for(Ticket__c t : newTickets){
        ticketIds.add(t.Id);
    }
    updateTicket(ticketIds);
}

public static void updateTicket(List<ID> newTickets){
    ID TicketId = newTickets[0];
    
    Ticket__c ticketsFromTrigger = [SELECT ID, Created_Date__c, Responded_Date__c, Resolved_Date__c, Total_Responded_Hours__c, Total_Resolved_Hours__c FROM Ticket__c
                      WHERE ID=: newTickets];
    
    BusinessHours bh = [SELECT ID FROM BusinessHours WHERE IsDefault=true];
    
    list<Ticket__c> ticketsToUpdate = new List<Ticket__c>();
    
        DateTime create = ticketsFromTrigger.Created_Date__c;
        DateTime Respond = ticketsFromTrigger.Responded_Date__c;
        DateTime Resolve = ticketsFromTrigger.Resolved_Date__c;
        if(ticketsFromTrigger.Responded_Date__c != null){
            Long RespondDiff = BusinessHours.diff(bh.id, create, Respond);
            Double hours = RespondDiff / 3600000;
            
            Long RespondDiff2 = BusinessHours.diff(bh.Id, create, Respond);
            Decimal de = BusinessHours.diff(bh.Id, create, Resolve);
            de = ((de/(1*60*60*1000))-(RespondDiff/(1*60*60*1000)));
            
            Decimal DeRespond = ((de/100)*60);
            ticketsFromTrigger.Total_Responded_Hours__c = ((RespondDiff/(1*60*60*1000))+DeRespond);
        }
        If(ticketsFromTrigger.Resolved_Date__c != null){
            Long ResolveDiff = BusinessHours.diff(bh.Id, create, Resolve);
            Double hours2 = ResolveDiff/3600000;
            
            Long ResolvedDiff2 = BusinessHours.diff(bh.Id, create, Resolve);
            Decimal de1 = BusinessHours.diff(bh.Id, create, Resolve);
            de1 = ((de1/(1*60*60*1000))-(ResolvedDiff2/(1*60*60*1000)));
            
            Decimal de2 = ((de1/100)*60);
            ticketsFromTrigger.Total_Resolved_Hours__c = ((ResolvedDiff2/(1*60*60*1000))+de2);
        }
        ticketsToUpdate.add(ticketsFromTrigger);

    if(ticketsToUpdate.size()>0){
        update ticketsToUpdate;
    }
}

}

Images from record where math is not correct. Anyone that can help give a second set of eyes and advise what changes need to be made to make the math work?

enter image description here

enter image description here

2

As per the BusinessHours documentation:

All methods are static.

So you should use: BusinessHours.diff(bh.id, create, Respond) instead of trying to use a method from the instance you queried, which does not exist.

6
  • Thank you, that got me further and also told me to move this from a trigger to a process and I have update the code to make that happen and now the diff populates on the record, however it is a very long number and not in hours with 2 decimal places, So I was anticipating that 25.45 hours would be shown but I get 91,295,000.00. Any thoughts on the equation and where to place that equation in the code? Thanks
    – Shawn
    Oct 15 '20 at 15:57
  • The method returns a Long, not an integer. This long should be converted to a date/time type before assigning to your values. You should get correct results then. Oct 15 '20 at 16:52
  • I am a little confused, how do you convert a long to a date time sir? Thanks again and sorry for my newbie questions here...
    – Shawn
    Oct 15 '20 at 17:03
  • And then how does that help me determine the amount of hours and minutes between the two dates that were passed into the diff, if we just convert the diff to a date time???? Seems like I will be missing two other dates to make the determination??? Please help
    – Shawn
    Oct 15 '20 at 17:13
  • The long returned is a representation of the difference between the two datetimes in miliseconds. Just do some math and you can get the minutes/seconds/hours/whatever you need to add to the lower date and assign to your field. Oct 15 '20 at 23:17

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.