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 

Error with Datetime field

Hi, I'm getting this error message, Error:Apex trigger CaseResolutionTimeTrigger caused an unexpected exception, contact your administrator: CaseResolutionTimeTrigger: execution of BeforeUpdate caused by: System.NullPointerException: End date cannot be null: (System Code) ., So I was wondering how could I make this work so that even when I have entered the date to the field and I want to empy the date field it won't shout error.

 

trigger CaseResolutionTimeTrigger on Case (before update) {
    
    if (Trigger.isUpdate) {
        
        for (Case updatedCase:System.Trigger.new) {
            System.Debug('CaseResolutionTimeTrigger: CASE ID: ' + updatedCase.Id);           
                        
            RecordType recordType = [select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
            
            System.Debug('CaseResolutionTimeTrigger: FETCHED RECORD TYPE: ' + recordType.Id );
            System.Debug('CaseResolutionTimeTrigger: 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('CaseResolutionTimeTrigger: OLD STATUS: ' + oldCase.Status);
                System.Debug('CaseResolutionTimeTrigger: NEW STATUS: ' + updatedCase.Status);

                  if (updatedCase.Date_and_time_of_resolution__c!= null && updatedCase.Related_Setup__c != null && updatedCase.Resolution_time__c == null ||(oldCase.Date_and_time_of_resolution__c!= updatedCase.Date_and_time_of_resolution__c)) {
                    
                    //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('CaseResolutionTimeTrigger: 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('CaseResolutionTimeTrigger: HOURS TO USE: ' + hoursToUse);
                                        
                     //Double ResolutionTime = DateTime.Now().GetTime() - Initial_Response__c.GetTime() - Date_and_time_of_resolution__c.GetTime();
                     Double resolutionTime =     BusinessHours.diff(hoursToUse,updatedCase.Initial_Response__c,updatedCase.Date_and_time_of_resolution__c);
                     Double rmMins = resolutionTime / 60000.0;
                     Double rmHrs = resolutionTime / 60000.0 / 60;
                     Double rmDays = resolutionTime / 60000.0 / 60 / 24;

                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (ms): ' + resolutionTime);
                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (mins): ' + rmMins);
                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (hrs): ' + rmHrs);
                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (days): ' + rmDays);                     
                        
                     updatedCase.Resolution_time__c = rmMins;
                    // update updatedCase
                     
                  } else {
                        
                     System.Debug('SETUP NOT FOUND: ' + updatedCase.Related_Setup__c);
                     }
                   }
                }
            }
        }
    }
}

kiranmutturukiranmutturu
trigger CaseResolutionTimeTrigger on Case (before update) {
    
    if (Trigger.isUpdate) {

RecordType recordType = Schema.SObjectType.Case.RecordTypeInfosByName.get('Support').RecordTypeId;
        
        for (Case updatedCase:System.Trigger.new) {
            System.Debug('CaseResolutionTimeTrigger:

 CASE ID: ' + updatedCase.Id);           
                        

//RecordType recordType = [select Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];// You are not supposed to use a soql inside loops

			
			
            System.Debug('CaseResolutionTimeTrigger: FETCHED RECORD TYPE: ' + recordType.Id );
            System.Debug('CaseResolutionTimeTrigger: 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('CaseResolutionTimeTrigger: OLD STATUS: ' + oldCase.Status);
                System.Debug('CaseResolutionTimeTrigger: NEW STATUS: ' + updatedCase.Status);

                  if (updatedCase.Date_and_time_of_resolution__c!= null && updatedCase.Related_Setup__c != null && updatedCase.Resolution_time__c == null ||(

updatedCase.Date_and_time_of_resolution__c != null &&

oldCase.Date_and_time_of_resolution__c!= updatedCase.Date_and_time_of_resolution__c)) {
                    
                    //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]; //here also need to avoid soql inside for loop

                      System.Debug('CaseResolutionTimeTrigger: 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('CaseResolutionTimeTrigger: HOURS TO USE: ' + hoursToUse);
                                        
                     //Double ResolutionTime = DateTime.Now().GetTime() - Initial_Response__c.GetTime() - Date_and_time_of_resolution__c.GetTime();
                     Double resolutionTime =     BusinessHours.diff(hoursToUse,updatedCase.Initial_Response__c,updatedCase.Date_and_time_of_resolution__c);
                     Double rmMins = resolutionTime / 60000.0;
                     Double rmHrs = resolutionTime / 60000.0 / 60;
                     Double rmDays = resolutionTime / 60000.0 / 60 / 24;

                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (ms): ' + resolutionTime);
                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (mins): ' + rmMins);
                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (hrs): ' + rmHrs);
                     System.Debug('CaseResolutionTimeTrigger: RESPONSE TIME (days): ' + rmDays);                     
                        
                     updatedCase.Resolution_time__c = rmMins;
                    // update updatedCase
                     
                  } else {
                        
                     System.Debug('SETUP NOT FOUND: ' + updatedCase.Related_Setup__c);
                     }
                   }
                }
            }
        }
    }
}

  This trigger needs bulkification, also not in the best practices zone...Help doc is here

LaaralLaaral

This bulkifying is a new thing for me because I've been coding with apex for 2 months now, so does this look right if I do this :

 

trigger CaseResolutionTimeTrigger on Case (before update) {
    
    if (Trigger.isUpdate) {
        
        RecordType recordType = Schema.SObjectType.Case.RecordTypeInfosByName.get('Support').RecordTypeId;
        List<RecordType> recordType =[SELECT Id, Name from RecordType where SObjectType = 'Case' and Name = 'Support' LIMIT 1];
        
        for (Case updatedCase:System.Trigger.new) {
            System.Debug('CaseResolutionTimeTrigger:CASE ID: ' + updatedCase.Id);           
            
            System.Debug('CaseResolutionTimeTrigger: FETCHED RECORD TYPE: ' + recordType.Id );
            System.Debug('CaseResolutionTimeTrigger: RECORD TYPE IN CASE: ' + updatedCase.RecordType.Id );