[rt-users] how to access content for an attachment in scrip?

Thomas Sibley trs at bestpractical.com
Thu Jul 7 20:25:00 EDT 2011


On 07/07/2011 07:21 PM, John Alberts wrote:
> Thanks for the tip.  It led me in the right direction and I ended up with
> this, which finally works.
> 
> my $T_Obj = $self->TicketObj;
>    my $a = RT::Attachments->new($self->TransactionObj->CurrentUser);
>    my $b = RT::Attachments->new($self->TransactionObj->CurrentUser);
>    my $AttachObj = $self->TransactionObj->Attachments;
>    while ( $a = $AttachObj->Next ) {
>   $b = $a;
>   next unless $a->Filename eq "env-vars.txt";
>            }
>    my $content = $b->Content;

The above will fail when env-vars.txt isn't the last attachment RT
parsed.  Try this (untested) much simpler version:

    my $attachments = $self->TransactionObj->Attachments;
    my $content;
    while (my $attach = $attachments->Next) {
        if ($attach->Filename eq "env-vars.txt") {
            $content = $attach->Content;
            last; # we found it, no need to keep looking
        }
    }
    # use $content for whatever you'd like here

There are better, faster ways to do this than looping over the
attachments, but looping is the easiest to understand.

Thomas



More information about the rt-users mailing list