CustomActionSnippets
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;
If you need to grab a given ticket's parent(s), child(ren), or other related tickets.
my $ParentTickets = $self->TicketObj->MemberOf; my $FirstParentTicketLink = $ParentTickets->Next; my $FirstParentTicket = $FirstParentTicketLink->TargetObj; my $SecondParentTicketLink = $ParentTickets->Next; my $SecondParentTicket = $SecondParentTicketLink->TargetObj; my $DependsOnTickets = $self->TicketObj->DependsOn; my $FirstDependsOnTicketLink = $DependsOnTickets->Next; my $FirstDependsOnTicket = $FirstDependsOnTicketLink->TargetObj; my $SecondDependsOnTicketLink = $DependsOnTickets->Next; my $SecondDependsOnTicket = $SecondDependsOnTicketLink->TargetObj; my $RefersToTickets = $self->TicketObj->RefersTo; my $FirstRefersToTicketLink = $RefersToTickets->Next; my $FirstRefersToTicket = $FirstRefersToTicketLink->TargetObj; my $SecondRefersToTicketLink = $RefersToTickets->Next; my $SecondRefersToTicket = $SecondRefersToTicketLink->TargetObj; my $ChildTickets = $self->TicketObj->Members; my $FirstChildTicketLink = $ChildTickets->Next; my $FirstChildTicket = $FirstChildTicketLink->BaseObj; my $SecondChildTicketLink = $ChildTickets->Next; my $SecondChildTicket = $SecondChildTicketLink->BaseObj; my $DependedOnByTickets = $self->TicketObj->DependedOnBy; my $FirstDependedOnByTicketLink = $DependedOnByTickets->Next; my $FirstDependedOnByTicket = $FirstDependedOnByTicketLink->BaseObj; my $SecondDependedOnByTicketLink = $DependedOnByTickets->Next; my $SecondDependedOnByTicket = $SecondDependedOnByTicketLink->BaseObj; my $ReferredToByTickets = $self->TicketObj->ReferredToBy; my $FirstReferredToByTicketLink = $ReferredToByTickets->Next; my $FirstReferredToByTicket = $FirstReferredToByTicketLink->BaseObj; my $SecondReferredToByTicketLink = $ReferredToByTickets->Next; my $SecondReferredToByTicket = $SecondReferredToByTicketLink->BaseObj;
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;