CustomActionSnippets

From Request Tracker Wiki
Revision as of 17:42, 30 April 2013 by 128.223.31.192 (talk) (Added "get related tickets" section.)
Jump to navigation Jump to search

Introduction

This page introduce you with some code that can be used in ScripActions and is part of CodeSnippets series of articles.

Custom actions specifics

You better read WriteCustomAction before to understand basics.

Code snippets

Change ticket's queue

my $TargetQueueName = 'MyQueue';
my ($status, $msg) = $TicketObj->SetQueue( $TargetQueueName );
...

Set current actor as Ticket owner

my $Actor = $self->TransactionObj->CreatorObj->Id;
if( $Actor != $self->TicketObj->OwnerObj->Id ) {
  $RT::Logger->info("Auto assign ticket #". $self->TicketObj->id ." to user #". $Actor );

  my ($status, $msg) = $self->TicketObj->SetOwner( $Actor );
  unless( $status ) {
    die "Error: $msg";
  }
}
return 1;

Set owner to nobody

We want to be able to assign cases to unprivileged customers, once they have replied the case should be released:

$self->TicketObj->SetOwner( $RT::Nobody->id );
return 1;
 

Get related tickets

If you need to grab a given tickets parent(s), child(ren), or other related tickets.


my $ParentTickets = $self->TicketObj->MemberOf;
my $FirstParentTicket = $ParentTickets->Next;
my $FirstParentTicketId = $FirstParentTicket->TargetObj->id;
my $SecondParentTicket = $ParentTickets->Next;
my $SecondParentTicketId = $SecondParentTicket->TargetObj->id;

my $DependsOnTickets = $self->TicketObj->DependsOn;
my $FirstDependsOnTicket = $DependsOnTickets->Next;
my $FirstDependsOnTicketId = $FirstDependsOnTicket->TargetObj->id;
my $SecondDependsOnTicket = $DependsOnTickets->Next;
my $SecondDependsOnTicketId = $SecondDependsOnTicket->TargetObj->id;

my $RefersToTickets = $self->TicketObj->RefersTo;
my $FirstRefersToTicket = $RefersToTickets->Next;
my $FirstRefersToTicketId = $FirstRefersToTicket->TargetObj->id;
my $SecondRefersToTicket = $RefersToTickets->Next;
my $SecondRefersToTicketId = $SecondRefersToTicket->TargetObj->id;

my $ChildTickets = $self->TicketObj->Members;
my $FirstChildTicket = $ChildTickets->Next;
my $FirstChildTicketId = $FirstChildTicket->BaseObj->id;
my $SecondChildTicket = $ChildTickets->Next;
my $SecondChildTicketId = $SecondChildTicket->BaseObj->id;

my $DependedOnByTickets = $self->TicketObj->DependedOnBy;
my $FirstDependedOnByTicket = $DependedOnByTickets->Next;
my $FirstDependedOnByTicketId = $FirstDependedOnByTicket->BaseObj->id;
my $SecondDependedOnByTicket = $DependedOnByTickets->Next;
my $SecondDependedOnByTicketId = $SecondDependedOnByTicket->BaseObj->id;

my $ReferredToByTickets = $self->TicketObj->ReferredToBy;
my $FirstReferredToByTicket = $ReferredToByTickets->Next;
my $FirstReferredToByTicketId = $FirstReferredToByTicket->BaseObj->id;
my $SecondReferredToByTicket = $ReferredToByTickets->Next;
my $SecondReferredToByTicketId = $SecondReferredToByTicket->BaseObj->id;

Log custom fields and standard fields to syslog

We want to log all custom and standard fields to syslog, so we can generate reports and audit logs, i.e. with Splunk. This would go in transaction batch

#On ticket creation, log standard and custom fields
my $ticket = $self->TicketObj;

#create a hash with all the standard fields we want
my %tkt_props;
$tkt_props{ticket} = $self->TicketObj->id;
$tkt_props{queue} = $self->TicketObj->QueueObj->Name;
$tkt_props{status} = $self->TicketObj->Status;
$tkt_props{subject} = $self->TicketObj->Subject;
$tkt_props{priority} = $self->TicketObj->Priority;
$tkt_props{owner} = $self->TicketObj->OwnerObj->Name;
$tkt_props{requestor_email} = $ticket->RequestorAddresses;

#add custom fields to the hash as well
my $CustomFields = $ticket->QueueObj->TicketCustomFields();
while (my $CustomField = $CustomFields->Next()) {
	my $name = $CustomField->Name;
	my $val = $ticket->FirstCustomFieldValue($name);
	$tkt_props{$name} = $val;
}

#create the string to log: RT4 transaction detected: field="value" field_2="value with spaces"
my $log_string = "RT4 transaction detected:";
foreach my $k (sort keys %tkt_props) {
	my $val = $tkt_props{$k}; #easier than each if you want them sorted
	$k =~ s/\s+/_/g; #remove spaces from name field and replace with _
	$k =~ tr/"//; #there really shouldn't be any quotes in the name, but strip them anyway
	$val =~ s/\s+/ /; #replace all whitespace with a single space char for Splunk
	$val =~ tr/"/'/; #replace double quotes with single for Splunk
	$log_string .= qq| $k="$val"|; #wrap values in quotes
}

$RT::Logger->info( $log_string );
return 1;​ 

Log transaction content to syslog

We want to log all content in tickets to syslog, so we can generate reports and audit logs, i.e. with Splunk. This would go in transaction create.

#log content of last transaction, iff it was a create, comment, or correspond
my $ticket_id = $self->TicketObj->id;
my $Transactions = $self->TicketObj->Transactions;
$Transactions->OrderBy(FIELD => 'Created', ORDER => 'DESC');
my $Transaction = $Transactions->Next;
my $transaction_type = $Transaction->Type;
#$RT::Logger->debug( "RT4 ticket=$ticket_id transaction_type=$transaction_type" );
if ( $Transaction->Type =~ /^(Correspond|Comment|EmailRecord|CommentEmailRecord|Create|Resolve)$/ && $Transaction->Content ) {
	#might need to strip html tags here?
	my $content = $Transaction->Content;
	$content =~ s/\s+/ /g; #replace all spaces with a single space char
	$content =~ tr/"/'/; #replace all double quotes with a single quote for splunk field extraction
	my $log_string = qq|RT4 message detected: ticket="$ticket_id" transaction_type="$transaction_type" content="$content"|;
	$RT::Logger->info( $log_string );
}
return 1;

More examples

CodeSnippets

See also

WriteCustomAction, CodeSnippets, Scrips