AutoSetOwner: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
(Added line to prevent a ticket being assigned to an unprivileged user.)
m (Added syntax highlighting)
 
(3 intermediate revisions by 2 users not shown)
Line 5: Line 5:
This custom action sets owner of the ticket to the current user if nobody yet owns the ticket. You can use this scrip action with any condition you want, for eg <code>On Resolve</code>.
This custom action sets owner of the ticket to the current user if nobody yet owns the ticket. You can use this scrip action with any condition you want, for eg <code>On Resolve</code>.


<syntaxhighlight lang="perl" line="1" >
  Description: AutoSetOwner
  Description: AutoSetOwner
  Condition: On Resolve
  Condition: On Resolve
Line 12: Line 13:
  Custom action cleanup code:
  Custom action cleanup code:
   # get actor ID
   # get actor ID
   my $Actor = $self-&gt;TransactionObj-&gt;Creator;
   my $Actor = $self->TransactionObj->Creator;
   # if actor is RT_SystemUser then get out of here
   # if actor is RT_SystemUser then get out of here
   return 1 if $Actor == $RT::SystemUser-&gt;id;
   return 1 if $Actor == $RT::SystemUser->id;
   # prevents a ticket being assigned to an unprivileged user, comment out if you want this
   # prevents a ticket being assigned to an unprivileged user, comment out if you want this
   return 1 unless $self->TransactionObj->CreatorObj->Privileged;
   return 1 unless $self->TransactionObj->CreatorObj->Privileged;
   # get out unless ticket owner is nobody
   # get out unless ticket owner is nobody
   return 1 unless $self-&gt;TicketObj-&gt;Owner == $RT::Nobody-&gt;id;
   return 1 unless $self->TicketObj->Owner == $RT::Nobody->id;
   # ok, try to change owner
   # ok, try to change owner
   $RT::Logger-&gt;info("Auto assign ticket #". $self-&gt;TicketObj-&gt;id ." to user #". $Actor );
   $RT::Logger->info("Auto assign ticket #". $self->TicketObj->id ." to user #". $Actor );
   my ($status, $msg) = $self-&gt;TicketObj-&gt;SetOwner( $Actor );
   my ($status, $msg) = $self->TicketObj->SetOwner( $Actor );
   unless( $status ) {
   unless( $status ) {
     $RT::Logger-&gt;error( "Impossible to assign the ticket to $Actor: $msg" );
     $RT::Logger->error( "Impossible to assign the ticket to $Actor: $msg" );
     return undef;
     return undef;
   }
   }
   return 1;
   return 1;
  Template: Global template: Blank
  Template: Global template: Blank
</syntaxhighlight>


N.B. If you do not have [[NotifyActor]] set and have the global default "On Owner Change Notify Owner with template Owner change" scrip, the above will notify the actor of the ticket being assigned to them as the [[SetOwner]] function creates a new transaction. To avoid this, use the following to set the ticket ownership:
N.B. If you do not have [[NotifyActor]] set and have the global default "On Owner Change Notify Owner with template Owner change" scrip, the above will notify the actor of the ticket being assigned to them as the [[SetOwner]] function creates a new transaction. To avoid this, use the following to set the ticket ownership:
Line 41: Line 39:
<u>Changelog:</u>
<u>Changelog:</u>


  20050525/Rejo  Added "Condition" line
  '''20050525/Rejo''' Added "Condition" line
   
   
  20060105/HaimDimer Translated the remaning French into English.
  '''20060105/HaimDimer''' Translated the remaning French into English.<br />
Changed the severity of the error message from "warning" to "error"
Changed the severity of the error message from "warning" to "error"
 
'''20181105/Cris70''' Fixed formatting of comments in code
 
'''20191001/Cris70''' Added syntax highlighting


A variation on this theme is [[AutoSetOwnerIfAdminCc]]
A variation on this theme is [[AutoSetOwnerIfAdminCc]]


Another variation is [[AutoSetOwnerForQueue]]
Another variation is [[AutoSetOwnerForQueue]]

Latest revision as of 03:43, 1 October 2019

Contributed by NicolasC

Overview

This custom action sets owner of the ticket to the current user if nobody yet owns the ticket. You can use this scrip action with any condition you want, for eg On Resolve.

 Description: AutoSetOwner
 Condition: On Resolve
 Action: User Defined
 Custom action preparation code:
   return 1;
 Custom action cleanup code:
   # get actor ID
   my $Actor = $self->TransactionObj->Creator;
   # if actor is RT_SystemUser then get out of here
   return 1 if $Actor == $RT::SystemUser->id;
   # prevents a ticket being assigned to an unprivileged user, comment out if you want this
   return 1 unless $self->TransactionObj->CreatorObj->Privileged;
   # get out unless ticket owner is nobody
   return 1 unless $self->TicketObj->Owner == $RT::Nobody->id;
   # ok, try to change owner
   $RT::Logger->info("Auto assign ticket #". $self->TicketObj->id ." to user #". $Actor );
   my ($status, $msg) = $self->TicketObj->SetOwner( $Actor );
   unless( $status ) {
     $RT::Logger->error( "Impossible to assign the ticket to $Actor: $msg" );
     return undef;
   }
   return 1;
 Template: Global template: Blank

N.B. If you do not have NotifyActor set and have the global default "On Owner Change Notify Owner with template Owner change" scrip, the above will notify the actor of the ticket being assigned to them as the SetOwner function creates a new transaction. To avoid this, use the following to set the ticket ownership:

$self->TicketObj->_Set(Field => 'Owner', Value => $Actor, RecordTransaction => 0);

This, as detailed on the WriteCustomAction page will not record the change as a transaction, thus preventing notification.

Changelog:

20050525/Rejo  Added "Condition" line

20060105/HaimDimer Translated the remaning French into English.

Changed the severity of the error message from "warning" to "error"

20181105/Cris70 Fixed formatting of comments in code
20191001/Cris70 Added syntax highlighting

A variation on this theme is AutoSetOwnerIfAdminCc

Another variation is AutoSetOwnerForQueue