You're not currently signed in.

Introduction

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

Custom conditions specifics

Read WriteCustomCondition to understand basics of writing conditions.

Code Snippets

Here is list of various custom conditions you can use.

On Create

return 0 unless $self->TransactionObj->Type eq "Create";
return 1;

On Create, but queue is not 'xxx'

Assume that you have a "Postmaster" queue, and you would prefer that the "On Create Autoreply To Requestors" global scrip would not autoreply to postmaster problems. However, it should autoreply for all other queues.

# If the type is "On Create" and the ticket is not headed for the Postmaster
# queue, return true (to carry out this scrip).
return($self->TransactionObj->Type eq "Create" &&
       $self->TicketObj->QueueObj->Name ne "Postmaster");

On Create with status resolved

If you create Ticket with status resolved then standard RT condition OnResolve wouldn't trigger. See also discussion.

return 0 unless $self->TransactionObj->Type eq "Create";
return 0 $self->TicketObj->Status eq 'resolved';
return 1;

On Correspond to Unowned Ticket

Here is an example that was found on the mailing list (message link).

I wasn't pleased with RT3's global scrip, "On Correspond Notify AdminCCs with Template Admin Correpondence" because it made for too much traffic. So I deleted that and added, "On Correspond Notify Owner with template Correspondence" through the web interface. I also needed something like, "On Correspond to Unowned Notify Admin Ccs W Admin Correspondence". So...

On the Add a scrip page, I used

Condition: User-defined 
Custom Condition: 

if ( $self->TransactionObj->Type eq "Correspond" && 
     $self->TicketObj->Owner == $RT::Nobody->Id )
{ 
  return 1; 
} else {
  return undef; 
}

and

Action: NotifyAdminCCs
Template: Global Template: Admin Correspondence 

(Credit to Peter Burkholder, UCAR)

on Status Change and Tickets with Subject containing "My test"

return 0 unless $self->TransactionObj->Type eq "Status";
return 0 unless $self->TicketObj->Subject =~ /My test/;
return 1;

on Status Change to "resolved" and Tickets with Subject containing "My test"

return 0 unless $self->TransactionObj->Type eq "Status";
return 0 unless $self->TransactionObj->NewValue eq "resolved";
return 0 unless $self->TicketObj->Subject =~ /My test/;
return 1;

on Status Change to "open" from "new", but only by hand (e.g: no take)

return 0 unless $self->TransactionObj->Type eq "Status";
return 0 unless $self->TransactionObj->NewValue eq "open";
return 0 unless $self->TransactionObj->OldValue eq "new";
return 0 unless $self->TransactionObj->Creator ne 1;
return 1;

on Resolve and Owner is not in the group

This scrip is used to manage a support queue that lets managers know when a support issue is resolved. It will not send a message if the ticket was resolved by a member in the SupportManagers group. It uses a custom template similar to the one on page p. 93 of RT Essentials.

return 0 unless ($self->TransactionObj->Type eq "Status");
return 0 unless ($self->TransactionObj->NewValue eq "resolved");

my $in_group = 'SupportManagers';

my $PrincipalObj = $self->TicketObj->OwnerObj->PrincipalObj;
my $GroupObj = RT::Group->new( $OwnerObj );
$GroupObj->LoadUserDefinedGroup( $in_group );
return 0 if ( $GroupObj->HasMemberRecursively( $PrincipalObj ) );

return 1;

On Queue Change

return 0 unless $self->TransactionObj->Type eq "Set";
return 0 unless $self->TransactionObj->Field eq "Queue";
return 1;

On Any Owner Change

return 0 unless $self->TransactionObj->Type eq "Set";
return 0 unless $self->TransactionObj->Field eq "Owner";
return 1;

On Owner Change from Nobody to Any

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 unless $txn->OldValue == $RT::Nobody->id;
return 1;

On Take

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 unless $txn->OldValue == $RT::Nobody->id;
return 0 unless $txn->NewValue == $txn->Creator;
return 1;

On Steal

See also OnStealEnhanced

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 if $txn->OldValue == $RT::Nobody->id;
return 0 unless $txn->NewValue == $txn->Creator;
return 1;

On Give

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 unless $txn->OldValue == $RT::Nobody->id;
return 0 if $txn->NewValue == $txn->Creator;
return 1;

On Give Up

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 unless $txn->OldValue == $txn->Creator;
return 0 unless $txn->NewValue == $RT::Nobody->id;
return 1;

On Assign

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 unless $txn->OldValue == $RT::Nobody->id;
return 0 if $txn->NewValue == $txn->Creator;
return 1;

On Re-Assign

my $txn = $self->TransactionObj;
return 0 unless $txn->Type eq "Set";
return 0 unless $txn->Field eq "Owner";
return 0 if $txn->OldValue == $txn->Creator;
return 0 if $txn->NewValue == $txn->Creator;
return 1;

On Requestor's action

return 1 if $self->TicketObj->IsWatcher(
    Type => 'Requestor', Email => 'foo@bar.com'
);
return 0;

On action by privileged

my $Actor = $self->TransactionObj->CreatorObj;
return 1 if $Actor->Privileged;
return 0;

On owner's action

my $Owner = $self->TicketObj->Owner;
return 1 unless $Actor->id == $Owner;
return 0;

On actions by a member of one or more particular groups

my @enforced_groups = ("Group 1","Group 2","etc");

my $c_user = $self->TransactionObj->CreatorObj;
my $GroupObj = RT::Group->new($RT::SystemUser); 
foreach (@enforced_groups) {
  my $GroupName = $_;
  $GroupObj->LoadUserDefinedGroup($GroupName); 
  next unless $GroupObj;
  my $userGroupObj = $GroupObj->UserMembersObj;
  while (my $userObj = $userGroupObj->Next) {
	  return 1 if $userObj->id eq $c_user->id;
  }
}

On Resolution (or other status) depending on the value of a custom field

use this custom condition to action on ticket resolution (or any other status) depending on the value of a 'choose one value' custom field

my $Ticket = $self->TicketObj; 

# substitute for the custom field name
my $TicketType = $Ticket->FirstCustomFieldValue('CustomFieldName'); 

# use whatever status is required
return 0 unless $Ticket->Status eq "resolved";

# substitute for the value you want to act on
return 0 unless $TicketType eq 'MyCustomFieldValue';
return 1;


add your own conditions

...