+1 vote
33 views
by (163k points)

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

1 Answer

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.
...