Can a scrip do a search like RT::Client::REST does?

My scrip looks for currently open Incidents that are related to each of the
IP addresses in CF.{IP} on an Incident Report. If it finds one it links
the Incident Report to that Incident thus making it unnecessary for someone
to do it manually later. This might be changed to CustomerID or something
later.

Anyway - The snippet from my scrip that I’d like to modify with something
more native instead of using RT::Client::REST is the following. Is there a
way to search for tickets without using RT::Client::REST? I’ve been
reading through page after page but I’m not able to figure this one out.
Is this the only way I’ll be able to do it? It works but it logs itself
in to RT via the web interface each time a new Incident Report comes in.

use RT::Client::REST;
use Error qw(:try);
my $rturl = “http://”.RT->Config->Get(‘WebDomain’);
my $rt = RT::Client::REST->new( server => $rturl, timeout => 30 );
my $user = ‘XXXXXX’;
my $pass = ‘XXxx11’;
try {
$rt->login(username => $user, password => $pass);
} catch Exception::Class::Base with {
die "problem logging in: ", shift->message;
};

my $ips = $self->TicketObj->CustomFieldValues( ‘IP’ );
while ( my $ipobj = $ips->Next ) {
my $ip = $ipobj->Content;
$RT::Logger->critical(“Searching for open incidents related to
“.$ip.”\n”);
my $query = qq/Queue = ‘Incidents’ AND Status = ‘open’ AND CF.{IP} =
‘$ip’/;
my @ids = $rt->search(
type => ‘ticket’,
query => $query,
);
foreach my $id (@ids) {
$RT::Logger->critical(“Linking to existing Incident #”.$id." for
“.$ip.”\n");
$self->TicketObj->AddLink( Type=>‘MemberOf’, Target=> $id );
}
}

Landon Stewart LandonStewart@Gmail.com

My scrip looks for currently open Incidents that are related to each of the
IP addresses in CF.{IP} on an Incident Report. If it finds one it links
the Incident Report to that Incident thus making it unnecessary for someone
to do it manually later. This might be changed to CustomerID or something
later.

Anyway - The snippet from my scrip that I’d like to modify with something
more native instead of using RT::Client::REST is the following. Is there a
way to search for tickets without using RT::Client::REST? I’ve been
reading through page after page but I’m not able to figure this one out.
Is this the only way I’ll be able to do it? It works but it logs itself
in to RT via the web interface each time a new Incident Report comes in.

use RT::Client::REST;

Hi Landon,

I think you should be able to use RT::Search from your scrip directly.
Check out the API docs.

Cheers,
Ken

Hi Landon,

I think you should be able to use RT::Search from your scrip directly.
Check out the API docs.

Thanks Ken. The document I found was at
http://bestpractical.com/rt/docs/4.0/RT/Search.html

I don’t know how to specify the query though… Maybe I’m looking at this
wrong? I’m not really clear on what $arg is for here either. I thought it
was a parameter, like maybe where one would specify the query, but in the
information below under Argument it says it returns the optional argument
associated with the search. I find most of the documentation for the API
confusing like this actually.

Landon Stewart LandonStewart@Gmail.com

Thanks Ken. The document I found was
at RT::Search - RT 4.0.25 Documentation - Best Practical

You don’t want RT::Search for this.

You want to use the RT::Tickets class using the ->FromSQL method, which
accepts TicketSQL. There are docs and plenty of examples in the RT source.

Thanks Ken. The document I found was
at RT::Search - RT 4.0.25 Documentation - Best Practical

You don’t want RT::Search for this.

You want to use the RT::Tickets class using the ->FromSQL method, which
accepts TicketSQL. There are docs and plenty of examples in the RT source.

Aah thank you! Quick example I found which helps a lot is as follows:

my $tickets = RT::Tickets->new(RT->SystemUser);
$tickets->FromSQL($tsql);
while (my $t = $tickets->Next) {
# do stuff with each ticket $t here
print $t->Subject, “\n”;

}

Landon Stewart LandonStewart@Gmail.com

.
… and in case anyone was curious the equivelent and much better looking
code to the RT::Client::REST snippet I provided earlier is:

my $ips = $self->TicketObj->CustomFieldValues( ‘IP’ );
while ( my $ipobj = $ips->Next ) {
my $ip = $ipobj->Content;
my $tsql = qq/Queue = ‘Incidents’ AND Status = ‘open’ AND CF.{IP} =
’$ip’/;
my $tickets = RT::Tickets->new(RT->SystemUser);
$tickets->FromSQL($tsql);
while (my $t = $tickets->Next) {
$self->TicketObj->AddLink( Type=>‘MemberOf’, Target=> $t->id );
}
}