+ Start a Discussion
VAHBVAHB 

BusinessHours difference between Closed and Created

I currently have this written which isn't working out because sometimes people reopen cases

trigger UpdateResoTCategor on Case (before update) {

  Double timeStat;
  BusinessHours bh = [select id from businesshours where isDefault = true];
  
  for (Case c : Trigger.new){
    Case oldCase = Trigger.oldMap.get(c.ID);
      
      if (oldCase.Status != 'Closed' && (c.Status == 'Closed' || c.Status == 'Escalated')){
        timeStat = BusinessHours.diff(bh.id, c.CreatedDate, System.now())/3600000.0;
           
        c.TimeBetweenCase__c = timeStat;    
      }    
  }
}

 I want to calculate the time difference between open and closed in BusinessHouses based on CreatedDateTime and ClosedDateTime but the problem is that isClosed and the ClosedDate don't actually get genereated til afterwards. I tried to do the same thing on after update and used the ClosedDate field but I keep getting errors on it: "execution of AfterUpdate caused by: System.FinalException: Record is read-only"

 

Does anyone have any ideas on how to do this?

 

Best Answer chosen by Admin (Salesforce Developers) 
stcforcestcforce

if the case is in the process of beign closed, isn't the current date ie. system.now() what you want?

All Answers

stcforcestcforce

not really sure but my initial thoughts are:

a) can you do this with a formula field?

b) implement the field within a visualforce page and show at point of render.

VAHBVAHB

Formula Field I saw some but they didn't calculate the businesshours in a really simple way and the main part of using this is just for that, the BusinessHours class is only allowed to be used in apex and that automatically removes nonbusiness hours, weekends, and holidays

 

I'm not too knowledgable in visualforce and the second option you said.

 

 

I thought this would be a pretty simple thing, surprised no one else has ran into the problem.

 

I think the main issue is using isClosed and CloseDate of an object because they get generated after update, and not before.

stcforcestcforce

if you are able to detect the close in the before trigger, you should be able to perform the operation (i would have thought). The error message you posted is associated with an after trigger (modification is not permitted). Can you clarify?

VAHBVAHB

I believe that if you use

 

before update

 

the isClosed and ClosedDate are not generated yet. I attempted to use these fields when case.Status = 'Closed' and it refers to a null object for the date.

I read somewhere that ClosedDate and isCLosed are filled after update so you can only use them afterwards so I tried doing it in after update Trigger and I got that error.

stcforcestcforce

if the case is in the process of beign closed, isn't the current date ie. system.now() what you want?

This was selected as the best answer
VAHBVAHB

Ya thats what I am doing right now, but then the way my code is its kind of weird because I dont want it to keep reupdating the field if the field I want to update already gets filled in, but then what happens is sometimes people reopen cases and then field shoudl update, which is why if I could somehow figure out how to access ClosedDate it would be easiest but I cant write a frickin stupid after update trigger without getting errors.

 

From what I have been reading people say it is because I am trying to edit the object I that was updated in after update or something and I have create a new instance or something, not really sure what to make of all of it.

 

Anyways, yes I am currently using SYstem.now() but it just falls short because of the possibility of someone reopening a case

VAHBVAHB

I suppose it would still work, I guess I will try it out. Thanks for helping