AddRefersToOnEqualCustomField

From Request Tracker Wiki
Jump to navigation Jump to search

This isn't really an action, but actually used as a condition which is also acting. Which probably means it could be written better, but here goes.

We use it to check for serial numbers on an incoming repair queue. If the serial numbers match, the unit to be repaired has been seen before, so create RefersTo links between them.

It also triggers if the custom field is changed, but makes no effort to delete any links.

 my $cfname = 'SerialNumber';
 my $cf = RT::CustomField->new(RT->SystemUser);
 $cf->LoadByName(Name => $cfname);
 my $cfid = $cf->Id();
 
 # If create or change to custom field
 unless (
  ( $self->TransactionObj->Type eq "CustomField"
    &&  $self->TransactionObj->Field == $cfid )
  ||  $self->TransactionObj->Type eq "Create"
  ) {
    return 0;
 }
 
 my $sn = $self->TicketObj->FirstCustomFieldValue($cfname);
 
 my $tickets = new RT::Tickets(RT->SystemUser);
 #$tickets->LimitQueue( VALUE => $queuename);
 $tickets->LimitCustomField(
   CUSTOMFIELD => $cfid,
     OPERATOR => '=',
     VALUE => $sn
   );
 
 my $i=0;
 while (my $ticket = $tickets->Next) {
   if ($ticket->id != $self->TicketObj->id) {
     $i++;
     $self->TicketObj->AddLink(Type=>'RefersTo',Target=>$ticket->id);
   }
 }
 
 # This means you can use it as a condition and have another action if it did something
 unless ($i > 0) {
   return 0;
 }
 
 1;