[rt-devel] retrieving Keywords

Rich Lafferty rich+rt at lafferty.ca
Fri Sep 27 23:13:55 EDT 2002

On Fri, Sep 27, 2002 at 03:33:25PM -0700, Grant Miller <grant at pico.apple.com> wrote:
> I need a little help with my perl script.  I'm trying to write a function
> that when passed a Ticket id, it returns the keywords in that ticket.
> I'm having difficulty figuring out how to get the keywords from a ticket id.
> sub getkeyword {
>     my($keywordid) = shift;
>     my($statement,$sth, at row,$keyname,$keyparent) = '';
>     $statement = "select * from Keywords where id='$keywordid'";
>     $sth = execute_sql_statement($statement);

I would strongly encourage you to use RT's API instead of doing direct
SQL access to the database. (Otherwise, you will end up essentially
cutting and pasting code from RT anyhow.) That's what it's there for,
after all.

Here's an example script that when called with a ticket ID will return
all of the keywords for that ticket:

------8<------ cut here ------8<------
#!/usr/bin/perl -w

use strict;
use lib "/opt/rt2/lib";
use lib "/opt/rt2/etc";
use RT::Ticket;
use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect GetCurrentUser);


my $Ticket = new RT::Ticket(GetCurrentUser());
$Ticket->Load($ARGV[0]) || die "Can't load ticket $ARGV[0]";

# cribbed from webrt/Ticket/Elements/ShowKeywordSelects:
my $KeywordSelects = $Ticket->QueueObj->KeywordSelects;

while ( my $KeywordSelect = $KeywordSelects->Next ) {
   print $KeywordSelect->Name . "\n";

   my $Keywords = $Ticket->KeywordsObj($KeywordSelect->Id);
   while (my $Keyword = $Keywords->Next) { 
       my $k = $Keyword->KeywordObj->RelativePath($KeywordSelect->KeywordObj);
       print "\t$k\n";
------8<------ cut here ------8<------



Rich Lafferty --------------+-----------------------------------------------
 Ottawa, Ontario, Canada    |  Save the Pacific Northwest Tree Octopus!
 http://www.lafferty.ca/    |    http://zapatopi.net/treeoctopus.html
rich at lafferty.ca -----------+-----------------------------------------------

More information about the Rt-devel mailing list