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
LaaralLaaral 

Date/Time field is updated

I have a problem with my trigger which concerns resolution time ( it includes time from open case = initial response time to when a case is closed = resolution time), but it won't  re-calculate the resolution minutes if the response time has been set before and I'll try to change the date/time again. How should I change my code?


Here's my trigger:

´trigger CaseResolutionMinutesTrigger on Case (before update) {
    
    if (Trigger.isUpdate) {
        
        for (Case updatedCase:System.Trigger.new) {
            System.Debug('CaseResolutionMinutesTrigger: CASE ID: ' + updatedCase.Id);           
                        
            RecordType recordType = [select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
            
            System.Debug('CaseResolutionMinutesTrigger: FETCHED RECORD TYPE: ' + recordType.Id );
            System.Debug('CaseResolutionMinutesTrigger: RECORD TYPE IN CASE: ' + updatedCase.RecordType.Id );
            
            // Get old Case data. We are also only interested in Support Cases.
            if (System.Trigger.oldMap.get(updatedCase.Id) != null /*&& updatedCase.RecordType == recordType*/ ) {
            
                Case oldCase = System.Trigger.oldMap.get(updatedCase.Id);            
                System.Debug('CaseResolutionMinutesTrigger: OLD STATUS: ' + oldCase.Status);
                System.Debug('CaseResolutionMinutesTrigger: NEW STATUS: ' + updatedCase.Status);
                  if (updatedCase.Resolution_time__c != null && updatedCase.Resolution_minutes__c == null && updatedCase.Related_Setup__c != null ) {
                    
                    // Related Setup
                    Setup__c relatedSetup = [select Id, Contract__c, Cost_Center__c, Name, Service_Availability__c from Setup__c where Id =: updatedCase.Related_Setup__c];
                    System.Debug('CaseResolutionMinutesTrigger: Related Setup: ' + relatedSetup.Name);
                    
                    // Get BusinessHours from Setup -> Contract-> Contract(Agreement) -> Business Hours
                    ServiceAvailability saHelper = new ServiceAvailability();
                    Id hoursToUse = saHelper.GetHoursToUse(updatedCase, relatedSetup);
                    if(hoursToUse != null ) {                                       
                        System.debug('CaseResolutionMinutesTrigger: HOURS TO USE: ' + hoursToUse);
                                        
                     //Double resolutionMinutes = Initial_Response__c.GetTime() - Resolution_time__c.GetTime();
                     Double resolutionMinutes = BusinessHours.diff(hoursToUse, updatedCase.Initial_Response__c, System.now());
                     Double rmMins = resolutionMinutes / 60000.0;
                     Double rmHrs = resolutionMinutes / 60000.0 / 60;
                     Double rmDays = resolutionMinutes / 60000.0 / 60 / 24;
                        
                     System.Debug('CaseResolutionMinutesTrigger: RESOLUTION MINUTES (ms): ' + resolutionMinutes);
                     System.Debug('CaseResolutionMinutesTrigger: RESOLUTION MINUTES (mins): ' + rmMins);
                     System.Debug('CaseResolutionMinutesTrigger: RESOLUTION MINUTES (hrs): ' + rmHrs);
                     System.Debug('CaseResolutionMinutesTrigger: RESOLUTION MINUTES (days): ' + rmDays);                     
                        
                     updatedCase.Resolution_minutes__c = rmMins;
                   
                     // update updatedCase;
                   } else { 
                        System.Debug('SETUP NOT FOUND: ' + updatedCase.Related_Setup__c);
                   }
                }
            }
        }
    }
}

k_bentsenk_bentsen

Your trigger is not re-calculating the resolution minutes because of the highlighted red clause in your condition statement that wraps the meat and potatoes of your code:

 

if (updatedCase.Resolution_time__c != null && updatedCase.Resolution_minutes__c == null && updatedCase.Related_Setup__c != null ) {

 

Since I'm going to assume that your Resolution Minutes field does not have a default value and is therefore NULL whenever a new case is created, its calculation is done on the first update only. After the trigger executes once, the Resolution Minutes field will at that point have value, and thus not pass the above conidition statement on any subsequent trigger calls from a record update.

 

Unless I'm missing something, perhaps the easiest solution would be to just remove that clause.

 

 

LaaralLaaral

Yes, I noticed that but the problem is even if I take that updatedCase.Resolution_minutes__c == null  off from the code it still doesn't re-calculate the resolution minutes if I change the resolution time. When I put a date/time to resolution time field for example 1.10.2013 7:50 and save. And If I want to change it afterwards to  29.09.2013 13:30 it doesn't re-calculate the resolution minutes field put keeps the same minutes which where calculated first time.

LaaralLaaral
Also If I take that clause away from the if- sentence it means that every change I make to a case ( even if it doesn't concern time/date fields) for example change status of a case from minor --> normal it updates the time to Resolution Minutes what the time it is then and it shouldn't do that. It should update the resolution minutes only when resolution time is changed and I should be able to change the resolution time to past time, not the time it is right now.
k_bentsenk_bentsen

That's odd that even after removing that clause, the resolution minutes isn't being re-calculated on subsequent record updates. Have you opened up your debug logs to see how far the trigger gets when you're changing the resolution time?

 

Another idea is to change the updatedCase.Resolution_minutes__c == null clause to updatedCase.Resolution_minutes__c != oldCase.Resolution_minutes__c