This is the full code for the Action I’m performing.
package RT::Action::SetTWOO;
use strict;
use warnings;
use base ‘RT::Action’;
use Math::Round;
sub Prepare {
Nothing to prepare
return 1;
}
sub Commit {
my $self = shift;
#=========================================================================================
my $ticket = $self->TicketObj; # Get the current ticket
my $transactions = $ticket->Transactions; # Get the ticket transaction list
#=========================================================================================
my $start_date = RT::Date->new($RT::SystemtUser); # Define the date that “Waiting on ourselves” was set
my $end_date = RT::Date->new($RT::SystemUser); # Define the date that ‘ends’ WOO
my $calc = 0; # 0 = false, 1 = true
my $no_end = 1; # 0 = false, 1 = true
my $twoo = 0; # Time Waiting On Ourselves
my $ttwoo = 0; # Total Time Waiting On Ourselves
my $convert = 86400; # Number of seconds in a day
#=========================================================================================
my $set_WOO = “changed to Ourselves”; # State has moved to Ourselves
my $waiting_on = “Waiting On”; # “Trigger string” to test on
my $reset_WOO = “Waiting On Ourselves changed to”; #State has been cleared from Ourselves
my $add_WOO = “Waiting On Ourselves added”; # Value was set for ticket, not automatically
my $delete_WOO = “Waiting On Ourselves deleted”; # CF was blanked out
#=========================================================================================
Limit the types of transactions we are intersted in
$transactions->Limit(FIELD => ‘Type’, OPERATOR => ‘LIKE’, VALUE => ‘%CustomField%’);
$transactions->Limit(ENTRYAGGREGATOR => “OR”, FIELD => ‘Type’, OPERATOR => ‘LIKE’, VALUE => ‘%Create%’);
$RT::Logger->debug("*** Current user is: " . $RT::SystemUser);
$RT::Logger->debug("*** BEGIN TXN DUMP");
while (my $trn = $transactions->Next)
{
# If the entry is the ticket being created
if (index($trn->Type,“Create”) != -1)
{
$start_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$calc = 0;
$no_end = 1;
$RT::Logger->debug("*** WE MADE A TICKET: " . $start_date);
}
# If the custom field was “Waiting On"
elsif (index($trn->Description,$waiting_on) != -1)
{
# If the state left WOO
if (index($trn->Description,$reset_WOO) != -1)
{
$end_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$calc = 1;
$no_end = 0;
$RT::Logger->debug(”*** NOT OUR PROBLEM ANYMORE: " . $end_date);
}
# If the state changed to WOO
elsif (index($trn->Description,$set_WOO) != -1)
{
$RT::Logger->debug("*** WAITING ON OUTSELVES");
$start_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$calc = 0;
$no_end = 1;
}
# If the state was manually set from an unknown state
elsif (index($trn->Description,$add_WOO) != -1)
{
$RT::Logger->debug("*** SOMEONE FORGOT TO SET THE FIELD");
$start_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$no_end = 1;
$calc = 0;
}
elsif (index($trn->Description,$delete_WOO) != -1)
{
$RT::Logger->debug("*** WOO DELETED");
$end_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$no_end = 0;
$calc = 1;
}
# Some other transition for Waitin On
else {
$RT::Logger->debug("*** WAITING ON IS SET TO SOMETHING ELSE");
$calc = 0;
}
# If we have an end date, calculate the time spent and add it to the total
if ($calc == 1)
{
my $temp_time = $end_date->Diff($start_date);
$ttwoo = $ttwoo + $temp_time;
$RT::Logger->debug("*** We spent " . $temp_time . " sec waiting on ourselves.");
}
}
}
If we made it this far with no end date, then we need to use the current date to calucate time
if ($no_end == 1)
{
$end_date->SetToNow;
$twoo = $end_date->Diff($start_date);
$ttwoo = $ttwoo + $twoo;
$RT::Logger->debug(“TWOO: " . $twoo . " in seconds”);
$RT::Logger->debug(“TTWOO: " . $ttwoo . " in seconds”);
$twoo = nearest(.01,$twoo/$convert);
$ttwoo = nearest(.01,$ttwoo/$convert);
}
$RT::Logger->debug(“TWOO: " . $twoo . " in days”);
$RT::Logger->debug(“TTWOO: " . $ttwoo . " in days”);
my ($status,$msg) = $ticket->AddCustomFieldValue(Field => 29, Value => $twoo);
$RT::Logger->debug(“1: Status: $status Msg: $msg”);
($status, $msg) = $ticket->AddCustomFieldValue(Field => 30, Value => $ttwoo);
$RT::Logger->debug(“2: Status: $status Msg: $msg”);
$RT::Logger->debug("*** TXN HISTORY ENDS ***");
}
1;