Difference between revisions of "AddRefersToOnEqualCustomField"

From Request Tracker Wiki
Jump to navigation Jump to search
m (2 revisions imported)
 
m
 
Line 5: Line 5:
It also triggers if the custom field is changed, but makes no effort to delete any links.
It also triggers if the custom field is changed, but makes no effort to delete any links.


  <nowiki>my $cfname = 'SerialNumber';
  <pre>
my $cfname = 'SerialNumber';
  my $cf = RT::CustomField-&gt;new(RT-&gt;SystemUser);
  my $cf = RT::CustomField-&gt;new(RT-&gt;SystemUser);
  $cf-&gt;LoadByName(Name =&gt; $cfname);
  $cf-&gt;LoadByName(Name =&gt; $cfname);
Line 43: Line 44:
   
   
  1;
  1;
</nowiki>
</pre>

Latest revision as of 20:51, 13 August 2016

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;