Scan-and-set

From Request Tracker Wiki
Jump to navigation Jump to search

#!/usr/bin/perl -w # # Sample script to scan RT ticket attachments for a 4 digit number # following the string "important number" and add the value to # a custom field called "CustomField" # # It's an example of scanning attachchments and setting a custom field # from an external Perl script # # author rmeden@yahoo.com # use strict; use lib ("/path/to/rt/lib"); # Change this to your RT lib path! use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc); use RT::Tickets; select STDERR; $|=1; select STDOUT; $|=1; # Clean our environment CleanEnv(); # Load the RT configuration RT::LoadConfig(); RT::Init(); my $tickets = new RT::Tickets($RT::SystemUser); # Used to store Ticket search results $tickets->LimitStatus(VALUE => 'new'); $tickets->LimitStatus(VALUE => 'open'); $tickets->LimitCustomField(CUSTOMFIELD => 'CustomField', OPERATOR => "is", VALUE => "NULL"); # Loop through tickets while (my $Ticket = $tickets->Next) { my $NUM=""; <code><pre> # # Scan attachments and keep latest value # my $txns = $Ticket-&gt;Transactions; while (my $txn = $txns-&gt;Next ) { my $attachments = RT::Attachments-&gt;new($txn-&gt;CurrentUser); $attachments-&gt;Limit( FIELD =&gt; 'TransactionID', VALUE =&gt; $txn-&gt;id ); $attachments-&gt;OrderBy( FIELD =&gt; 'Id', ORDER =&gt; 'ASC' ); while ( my $a = $attachments-&gt;Next ) { next unless $a-&gt;ContentEncoding eq "none"; $NUM=$1 if $a-&gt;Content =~ /important number (\d{4})/m; } } next unless $NUM; # nothing found? printf "Setting Ticket %d Important Number %d\n",$Ticket-&gt;id,$NUM; $Ticket-&gt;AddCustomFieldValue(Field=&gt;"CustomField",Value=&gt;$NUM,RecordTransaction=&gt;0); </pre></code> } # Ticket loop $RT::Handle->Disconnect(); exit;