You need to sign in to do that
Don't have an account?
Shawn 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:
Class Code:
Thanks for any help you can provide!
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!
Can you try using the hardcoded business hour id in diff method to narrow this issue down? Something like this
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
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
Class Code:
Example of record and field data: