Public paste
Scrip to assign user
By: Sue D. Nymme | Date: Jan 5 2011 20:23 | Format: None | Expires: never | Size: 2.17 KB | Hits: 831

  1. $RT::Handle->BeginTransaction;
  2.  
  3. my $old_owner_id = $self->TicketObj->_Value('Owner');
  4. $RT::Logger->debug(qq{EJR-SetOwner: old owner is "$old_owner_id".});
  5.  
  6. my $subject = $self->TicketObj->Subject;
  7. if ($subject =~ s{A (w+):? s+ (?:FW|RE): s* }{}ix )
  8. {
  9.     my $new_owner_name = $1;
  10.     $RT::Logger->debug(qq{EJR-SetOwner: new owner name is "$new_owner_name".});
  11.  
  12.     my $new_owner_obj = RT::User->new($self->CurrentUser);
  13.     $new_owner_obj->Load($new_owner_name);
  14.     my $new_owner_id = $new_owner_obj->Id or do
  15.     {
  16.         $RT::Logger->error(qq{EJR-SetOwner: Cannot load User "$new_owner_name".});
  17.         $RT::Handle->Rollback;
  18.         return (0, qq{User "$new_owner_name" does not exist.});
  19.     };
  20.     $RT::Logger->debug(qq{EJR-SetOwner: User id for $new_owner_name is $new_owner_id.});
  21.  
  22.     # Set the new owner
  23.     my ($rv,$msg) = $self->TicketObj->_Set(Field => 'Owner',
  24.                                            Value => $new_owner_id,
  25.                                            RecordTransaction => 0,
  26.                                            CheckACL => 0);
  27.     unless ($rv)
  28.     {
  29.         $RT::Logger->error(qq{Cannot assign ticket to "$new_owner_name": $msg});
  30.         $RT::Handle->Rollback;
  31.         return (0, qq{Cannot assign ticket to "$new_owner_name": $msg});
  32.     }
  33.     my $oid = $self->TicketObj->Owner;
  34.     $RT::Logger->debug(qq{EJR-SetOwner: Assigned owner ($oid).});
  35.  
  36.     # Change the subject line
  37.     ($rv,$msg) = $self->TicketObj->_Set(Field => 'Subject',
  38.                                         Value => $subject,
  39.                                         RecordTransaction => 0,
  40.                                         CheckACL => 0);
  41.     unless ($rv)
  42.     {
  43.         $RT::Logger->warning(qq{Cannot change ticket subject to "$subject": $msg});
  44.         $RT::Handle->Rollback;
  45.         return (0, 'Cannot change ticket subject');
  46.     }
  47.     $RT::Handle->Commit;
  48.     $RT::Logger->debug(qq{EJR-SetOwner: Assigned subject.});
  49. }
  50. else
  51. {
  52.     $RT::Logger->error(qq{Cannot parse ticket subject "$subject"});
  53.     $RT::Handle->Rollback;
  54.     return (0, 'Cannot parse ticket subject');
  55. }
  56. $RT::Logger->debug(qq{EJR-SetOwner: Success, apparently.});
  57. return 1;