<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7651.59">
<TITLE>Set TimeWorked automatically</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>Hi,<BR>
<BR>
Searching on past threads, I couldn't find how to set the "TimeWorked" automatically when a ticket was resolved, taking into account the "Started" and the "Resolved" dates. This prompted me to developed the following Scrip, which uses Date::Calc to find the date difference and update the TimeWorked field. Does RT already provide such an option?<BR>
<BR>
Thanks,<BR>
<BR>
Miguel<BR>
<BR>
Condition: On Resolve<BR>
Action: User Defined<BR>
Template: Global template: Blank<BR>
Stage: TransactionCreate<BR>
Custom action preparation code: 1;<BR>
Custom action cleanup code:<BR>
---------------<BR>
use Date::Calc qw(Delta_DHMS);<BR>
<BR>
my $time_elapsed = "0";<BR>
<BR>
my $date_started_string = $self->TicketObj->Started;<BR>
my $date_resolved_string = $self->TicketObj->Resolved;<BR>
<BR>
my (@date_started,@date_resolved);<BR>
<BR>
if ($date_started_string =~ /(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/) {<BR>
        @date_started = ($1, $2, $3, $4, $5, $6);<BR>
}<BR>
<BR>
if ($date_resolved_string =~ /(\d+)\-(\d+)\-(\d+)\s(\d+)\:(\d+)\:(\d+)/) {<BR>
        @date_resolved = ($1, $2, $3, $4, $5, $6);<BR>
}<BR>
<BR>
my @diff = Delta_DHMS(@date_started, @date_resolved);<BR>
<BR>
my $day_to_minutes = $diff[0] * 1440;<BR>
my $hour_to_minutes = $diff[1] * 60;<BR>
<BR>
my $time_elapsed_sum = $day_to_minutes + $hour_to_minutes + $diff[2];<BR>
<BR>
if($time_elapsed_sum > 0){<BR>
        $time_elapsed = qq($time_elapsed_sum\.$diff[3]);<BR>
}<BR>
<BR>
$RT::Logger->debug("Setting TimeWorked to: $time_elapsed");<BR>
<BR>
$self->TicketObj->SetTimeWorked( $time_elapsed );<BR>
---------------<BR>
<BR>
</FONT>
</P>

</BODY>
</HTML>