0

I need to catch the time when a case status is changed from one status to another. like a case is in new status now its change to working or a case is in working and when it is changed to escalated . I want the time between one case status to another . please Help me, How can i achieve this task???

I have written a trigger.. But there is some mistake i could not find... please Help me and modify the error.

I create an Object: CaseStatusDuration Three fields are there: 1>currStat 2>casNum 3>Duration__c(Field Type:Date/Time)

When a case status will change then the Duration between two status will be save to that object CaseStatusDuration in Duration field.

trigger statusUpdateTrigger1 on Case (after update) {
List<Case> cases = new List<Case>(trigger.new);

        for(Case c : cases)
            {


                if(c.status != trigger.oldMap.get(c.id).status){
                   DateTime d1=c.CreatedDate;

                  DateTime d2=c.LastModifiedDate;
                   decimal Hours = decimal.valueOf((d2.getTime() - d1.getTime()));
                   decimal hour1 = Hours/(1000*60*60);

                   Integer min1 = Integer.valueOf(Hours/(1000*60));
                   min1=math.mod(min1,60);


                   String str1=String.valueOf(hour1);
                   string[] str2=str1.split('\\.');
                   string hour=str2[0];
                   string min=string.valueOf(min1);

                   String currStat=c.Status;
                   String casNum=c.CaseNumber;
                   CaseStatusDuration__c obj =new CaseStatusDuration__c();
                   obj.Duration__c=hour+':'+min;

                   obj.casNum=c.caseNumber;
                   obj.Status__c=currStat;

                   insert obj;

                   }
            }

}

1

I am not sure why you would need a trigger for this. I think workflow rules would work just fine.

We created date/time fields for each of our statuses for reporting on time between each status. You could create the same for each of yours.

After the fields are created, create your workflow rules to capture the change in status. When the status equals the value you want, populate the respective date/time field with Now().


Edit to include code, since I am unsure of how to format the comment to show this.

This is the part of the link in the comment below that shows how to track days, hours, and minutes.

IF(
  Diff_c > 0 ,
  TEXT( FLOOR( Diff_c ) ) & " days "
  & TEXT( FLOOR( MOD( Diff_c * 24, 24 ) ) ) & " hours "
  & TEXT( ROUND( MOD( Diff_c * 24 * 60, 60 ), 0 ) ) & " minutes",
  ""
)
| improve this answer | |
  • Hi , Thanks for replying. I am very new to salesforce. I work with workflow as well. But i am facing difficulty . Can you kindly elaborate the process what u suggest... thanks in Advance. – Stuart Rozz Sep 17 '15 at 9:14
  • Here is what we did. Our Cases have 5 statuses; New, In Process, Waiting on Customer, Waiting on Employee, and Closed. We created 5 date/time fields; "Date of New Status", "Date of In Process Status", etc. Then, we created 5 workflow rules. That criteria is "Case: Status equals Waiting on Customer" and the condition is Evaluate the rule when a record is created, and any time it’s edited to subsequently meet criteria. Finally, we created an Immediate Action to update the new corresponding date/time field. In the formula part of that, enter Now(). – smckitrick Sep 17 '15 at 13:04
  • Thank you so much ..... Its working....Through workflow rule. But there i am facing a small problem , When i am substacting two Date/Time field it should return the time. But i only get the Number of days. Not getting the time. "LastModifiedDate-CreateDate"... Like it return only a number like 15....Created By Staurt Roz, 9/3/2015 7:09 AM, Last Modified By Staurt Roz, 9/18/2015 4:53 AM.. It returns only 15 , but not returning the min and hour........ – Stuart Rozz Sep 18 '15 at 13:39
  • I want to make sure that I understand what you are asking. Are you asking for a calculation that will tell you that, using your dates and times, 14 days, 21 hours, 44 minutes has passed? – smckitrick Sep 18 '15 at 14:07
  • This gave me a nice thing to learn myself. You can do this through something like a formula field. The following link will help you with this. developer.salesforce.com/docs/… I edited the answer to include the code specifically for tracking days and time calculations. – smckitrick Sep 18 '15 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.