function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shawn Reichner 33Shawn Reichner 33 

Using Business Hours in Apex Class Called From Trigger

Hello Awesome Devs!

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

Thanks for any help you can provide!
AnudeepAnudeep (Salesforce Developers) 
Hi Shawn, 

Can you try using the hardcoded business hour id in diff method to narrow this issue down? Something like this
 
List<BusinessHours> bhs=[select id from BusinessHours where IsDefault=false];
system.debug('bhs-->'+bhs);
static long x ;
Datetime startDate= DateTime.newInstance(2019, 11, 18, 3, 3, 3);
Datetime endDate = DateTime.newInstance(2019, 12, 18, 3, 3, 3);
x= BusinessHours.diff('01m2v000000wlJEAAY',startDate,endDate); 
system.debug('x--->'+x);

I also recommend putting a system debug to print values for Create, Respond, Resolve and then use the hardcoded values in the method

Notice that diff has the following syntax where businessHoursId is a string so I am suspecting a string value is not being passed and hence you are running into the issue you specified in the description
public static Long diff(String businessHoursId, Datetime startDate, Datetime endDate)

Let me know if this helps. If it does, please close this query by marking this as solved. It may help others in the community
Shawn Reichner 33Shawn Reichner 33
Anudeep, thank you for the advisement sir, and I was able to get past this yesterday, however I have a new challenge...something in my math is off and woudl really appreciate a second set of eyes on this if you would be so kind.  So I am now able to get the class to run but the hours are off. Please see the following example where we have a record that was responded to in 30 minutes, and resolved in one hour, and I woudl anticipate seeing .50 for the responded hours and 1 for the resolved hours, but both are showing as .60???  Thank you for taking a look and helping if you can.  If not, anyone else that can solve this????

Class 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;
        }
    }
}

Example of record and field data:
User-added image

User-added image​​​​​​​