TemplateSnippets

From Request Tracker Wiki
Revision as of 14:23, 8 February 2012 by 94.158.99.44 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Introduction

This page introduce you with some code that can used in notification Templates and is part of CodeSnippets series of articles.

Notification templates specifics

See also Template that describe why this text is about notifications and how can it be different for other actions.

In notification templates you have to enclose code into curly brackets:

{ ... here goes some code ... }

Ticket object is stored in $Ticket variable and transaction is stored in $Transaction, for example:

{ $Ticket->Status }

Each block of should result in some text (may be empty). In a simple case you just add code that results in text, like with status of a ticket above. For complex code use some local variable and append data to it:

{ my $out = '';
     $out .= "append some text line\n";
     foreach my $element (...) {
         $out .= "append more data $element\n";
     }
     $out; # don't forget to put $out at the end to return it
   }
   
   

That's it. Enjoy templating.

Code snippets

Basics

Ticket status

{ $Ticket->Status }

Queue name

{ $Ticket->QueueObj->Name }

Insert Queue email address for correspondence/comment

{ $Ticket->QueueObj->CorrespondAddress; }
{ $Ticket->QueueObj->CommentAddress; }

Puts content of the first transaction into email

{ $Ticket->Transactions->First->Content }

People

Get the email address of the user who created the ticket

{ $Ticket->CreatorObj->EmailAddress }

Get the email address of the user who created the transaction

{ $Transaction->CreatorObj->EmailAddress }

Get the name of the user who created the transaction

{ $Transaction->CreatorObj->Name }

Get the name of the user who last updated the ticket

{ $Ticket->LastUpdatedByObj->Name }

Get the email addresses of the Requestor(s)

{ $Ticket->RequestorAddresses }

Get the email addresses of the users added as CC or AdminCC to the ticket

{ $Ticket->CcAddresses }
{ $Ticket->AdminCcAddresses }

Requestors' names or other properties

Requestors: {
     # 'Requestors' may be replaced with 'Cc' or 'AdminCc'
     my $users = $Ticket->Requestors->UserMembersObj;
     my $output = '';
     while( my $user = $users->Next ) {
        $output .= ', ' if $output; # comma between values
        $output .= $user->Name; # or any other user's property
     }
     $output;
   }
   

Dates

Ticket creation time as formatted string:

{ $Ticket->CreatedAsString }

The same:

{ $Ticket->CreatedObj->AsString }

The same for Transaction

{ $Transaction->CreatedAsString }

Show the first date that is set:

This ticket was last active on {
    my $date;
    foreach my $method ( qw(ToldObj LastUpdatedObj CreatedObj) ) {
        $date = $Ticket->$method(); # get date
        last if $date->Unix > 0; # stop on first that is set
    }
    $date->AsString;
}

Complex

Add the number of tickets in queue:

There are currently {
  my $tickets = RT::Tickets->new( $RT::SystemUser );
  $tickets->FromSQL( "Status = 'new' AND Queue = ". $Ticket->Queue );
  $tickets->Count;
} ticket(s) awaiting processing before yours, please be patient.

Tips&Tricks

Don't show content if it's empty

{ if( my $tcc = $Ticket->CcAddresses ) { "Ticket Ccs: ". $tcc } }
{ if( my $qcc = $Ticket->QueueObj->CcAddresses ) { "Queue Ccs: ". $qcc } }
{ if( my $tacc = $Ticket->AdminCcAddresses ) { "Ticket AdminCcs: ". $tacc } }

See also

Template, CodeSnippets, CustomConditionSnippets