CustomConditionSnippets

From Request Tracker Wiki
Revision as of 13:29, 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 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 and variants

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

On Create, but queue is not 'xxx'

 
   return 0 unless $self->TransactionObj->Type eq "Create";
   return 0 if $self->TicketObj->QueueObj->Name eq "xxx";
   return 1;
   

On Create with status resolved

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

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

On Correspond/Comment and variants

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

On Correspond to Unowned Ticket

 return 0 unless $self->TransactionObj->Type eq "Correspond";
 return 0 if $self->TicketObj->Owner == $RT::Nobody->id;
 return 1;

Can be used in "On Correspond to Unowned Notify Admin Ccs With Admin Correspondence".

On Status Change and variants

For historical reasons it's more correct to write this condition like this:

my $txn = $self->TransactionObj;
my $type = $txn->Type;
return 0 unless $type eq "Status"
    || ( $type eq 'Set' && $txn->Field eq 'Status');
return 1;

On Status Change to "resolved"

my $txn = $self->TransactionObj;
my $type = $txn->Type;
return 0 unless $type eq "Status"
    || ( $type eq 'Set' && $txn->Field eq 'Status');
return 0 unless $txn->NewValue eq "resolved";
return 1;

on Status Change from "new" to "open"

my $txn = $self->TransactionObj;
my $type = $txn->Type;
return 0 unless $type eq "Status"
    || ( $type eq 'Set' && $txn->Field eq 'Status');
return 0 unless $txn->OldValue eq "new";
return 0 unless $txn->NewValue eq "open";
return 1;

On Queue Change

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

On Owner Change and variants

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;

== Checks with acting user (actor)

 my $actor = $self->TransactionObj->CreatorObj;

On action by privileged

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

On owner's action

my $actor = $self->TransactionObj->CreatorObj;
my $owner = $self->TicketObj->OwnerObj;
return 1 unless $actor->id == $owner->id;
return 0;

Checks with watchers and groups

When particular email address is in requestors

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

When actor is member of one or more particular groups

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

my $actor = $self->TransactionObj->CreatorObj;
foreach my $gname (@enforced_groups) {
  my $group = RT::Group->new( $self->CurrentUser );
  $group->LoadUserDefinedGroup( $gname );
  next unless $group->id;
  return 1 if $group->HasMemberRecursively( $actor->id );
}
return 0;

Owner is not in the group

my $in_group = 'SupportManagers';

my $owner = $self->TicketObj->OwnerObj;
my $group = RT::Group->new( $self->CurrentUser );
$group->LoadUserDefinedGroup( $in_group );
return 0 if $group->HasMemberRecursively( $owner->id );

return 1;

Custom Fields checks

When custom field value is X

 
   my $Ticket = $self->TicketObj;
  
   return 0 unless $self->TicketObj->FirstCustomFieldValue('CustomFieldName') eq 'X';
   return 1;
   

add your own conditions

...