[Rt-devel] No MessageId set for Attachments?

Petter Reinholdtsen pere at hungry.com
Fri Sep 3 17:32:14 EDT 2004


[Ruslan U. Zakirov]
>> +    chomp($MessageId);
> useless

While trying to map from message id to ticketid, I discovered that
this 'chomp' call is needed to avoid a trailing newline in all message
ids.

With this change, I can look up ticket IDs by message ids using this
SQL code:

  SELECT t.ticket from attachments a, transactions t
     WHERE a.messageid = '<message at id>'
     AND (a.contenttype = 'text/plain' or a.contenttype = 'text/html')
     AND a.parent = 0
     AND a.transactionid = t.id;

How do I represent this using the Perl objects available in RT?  I
posted some code I hoped would do this in January based on your input,
but didn't get any comments.  I am still very unskilled with this API,
so I do not understand why it fail.

This is my current test code.  What is wrong with it?  This is the
error message I get:

  [Fri Sep 3 21:27:18 2004] [crit]: Can't locate object method
    "Ticket" via package "RT::Attachments" at ./rt-msgid line 75.
   (/site/rt3/lib/RT.pm:257)

Here is the code:

#!/site/perl-5.8.4/bin/perl
#
# rt-msgid: test if a message id is in RT.

use lib ("/site/rt3/lib", "/site/rt3/local/lib");
use strict;
use warnings;
use English;
use RT::Interface::CLI qw(CleanEnv);
use RT::User;
use RT::Template;
use RT::Attachments;

my $msgid = shift || "<rt-3.2.1-18-472-2.7.16367876566103\@uio.no>";

CleanEnv();
RT::LoadConfig();
RT::Init();

my $UserObj = new RT::User(RT::SystemUser);

print "Ticket: ",FindByMsgId($msgid),"\n";

# Look up ticket IDs given MessageID of attachment

# SELECT t.ticket from attachments a, transactions t WHERE
#   a.messageid = '<rt-3.2.1-18-472-2.7.16367876566103 at uio.no>'
#   AND (a.contenttype = 'text/plain' or a.contenttype = 'text/html')
#   AND a.parent = 0
#   AND a.transactionid = t.id;

sub FindByMsgId {
    my $msgid = shift;

    print "Looking up msgid '$msgid'\n";

    my @ids = ();

    my $Attachs = RT::Attachments->new($RT::SystemUser);
    $Attachs->Limit( FIELD => 'MessageId',
                     OPERATOR => '=',
                     VALUE => $msgid
                     );
    $Attachs->Limit( FIELD => 'ContentType',
                     OPERATOR => '=',
                     VALUE => 'text/plain'
                     );
    $Attachs->Limit( FIELD => 'ContentType',
                     OPERATOR => '=',
                     VALUE => 'text/html'
                     );
    $Attachs->Limit( FIELD => 'Parent',
                     OPERATOR => '=',
                     VALUE => '0'
                     );
    my $trs = $Attachs->NewAlias('Transactions');
    my $tis = $Attachs->NewAlias('Tickets');

    $Attachs->Join( ALIAS1 => 'main',
                    FIELD1 => 'TransactionId',
                    ALIAS2 => $trs,
                    FIELD2 => 'id'
                    );

    $Attachs->Join( ALIAS1 => $trs,
                    FIELD1 => 'Ticket',
                    ALIAS2 => $tis,
                    FIELD2 => 'id'
                    );

    while (my $ticket = $Attachs->Next) {
        my $ticketid = $Attachs->Ticket;
        print "Found ID $ticketid\n";
        push(@ids, $ticketid) if $ticketid;
    }
    return @ids;
}


More information about the Rt-devel mailing list