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
LuciferLucifer 

Stop calculating Case Age in Business Hours when case is in a pending state.

Hi all,

 

I have to calculate the total time spent on a case. We have 5 status called new open pending working closed. So when ever the status is in working then the time is not supposed to be calculated. For this I have written a trigger which subtratcs the waiting time from total time. But looks like I had a problm. 

 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger caselifetimecalculator caused an unexpected exception, contact your administrator: caselifetimecalculator: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.caselifetimecalculator: line 6, column 1

 

 

Her is my code

 

trigger caselifetimecalculator on Case (before update) {
 
  for(Case c:Trigger.New){ 
   if(Trigger.oldMap.get(c.id).status != c.status)
       if(Trigger.oldMap.get(c.id).status !='Waiting' )
           c.total_activity_time__c +=  (datetime.now().getTime()- (c.CaseStatusLastModified__c == null ? c.CreatedDate.getTime() :c.CaseStatusLastModified__c.getTime()))/(1000*60);
       c.CaseStatusLastModified__c= datetime.now();
  }
 
}
 
 
 
Please create two fields on Case and expose"Total Activity Time" on layout.
 
 
 
     label                           type                     api                                           Help text
1.. "Total Activity time"    Number (10,5)  total_activity_time                        In seconds.       
2. "casestatuslastmodified" Date/time     CaseStatusLastModified

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Karthikeyan JayabalKarthikeyan Jayabal

I guess total_activity_time__c is null for the first time, so just add another condition to check if total_activity_time__c is null.

 

Also, there is a free appexchange app named Case Age In Business Hours which meets the same requirement.

All Answers

Karthikeyan JayabalKarthikeyan Jayabal

I guess total_activity_time__c is null for the first time, so just add another condition to check if total_activity_time__c is null.

 

Also, there is a free appexchange app named Case Age In Business Hours which meets the same requirement.

This was selected as the best answer
LuciferLucifer

Thanks dude, looks like the app is coming with apex code. I could use it or even base it for any code modifications. Thanks a bunch