OnMerge: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
(Adding categories)
(Changed code to handle multiple value fields and to actually merge, not overwrite)
Line 7: Line 7:
  return undef unless $txn->Field =~ /^MergedInto$/i;
  return undef unless $txn->Field =~ /^MergedInto$/i;
  return 1;
  return 1;


If you need to refer to the original ticket id in a template it is available as $Transaction->[[ObjectId]].
If you need to refer to the original ticket id in a template it is available as $Transaction->[[ObjectId]].
Line 24: Line 23:


  <nowiki>#Transaction Association
  <nowiki>#Transaction Association
  my $txn = $self-&gt;TransactionObj;
  my $txn = $self-&gt;TransactionObj;
 
 
  #Condition on Type
  #Condition on Type
  return undef unless $txn-&gt;Type =~ /^AddLink$/i;
  return undef unless $txn-&gt;Type =~ /^AddLink$/i;
  return undef unless $txn-&gt;Field =~ /^MergedInto$/i;
  return undef unless $txn-&gt;Field =~ /^MergedInto$/i;
 
 
  $RT::Logger-&gt;info('Merge is Occurring');
  $RT::Logger-&gt;info('Merge is Occurring');
 
 
  #Ticket Association
  #Ticket Association
  #The New Ticket your Merging into
  #The New Ticket your Merging into
  my $ticket    = $self-&gt;TicketObj;
  my $ticket    = $self-&gt;TicketObj;
 
 
  #The old Ticket your merging From
  #The old Ticket your merging From
  my $oldTicket = RT::Ticket-&gt;new($RT::SystemUser);
  my $oldTicket = RT::Ticket-&gt;new($RT::SystemUser);
    $oldTicket-&gt;LoadById($txn-&gt;ObjectId);
      $oldTicket-&gt;LoadById($txn-&gt;ObjectId);
 
 
  $RT::Logger-&gt;info('Merging '.$txn-&gt;ObjectId.' into '.$ticket-&gt;Id);
  $RT::Logger-&gt;info('Merging '.$txn-&gt;ObjectId.' into '.$ticket-&gt;Id);
 
 
  #Are we merging from the right queue?
  #Are we merging from the right queue?
  my $oldqueue = $oldTicket-&gt;Queue;
  my $oldqueue = $oldTicket-&gt;Queue;
  return undef unless $oldqueue == 36;
  return undef unless $oldqueue == 36;
 
 
  $RT::Logger-&gt;info('Merging From Lead Tracking Queue');
  $RT::Logger-&gt;info('Merging From Lead Tracking Queue');
 
 
  #Are we merging into an allowed queue?
  #Are we merging into an allowed queue?
  my @queues = qw(Enrollment/Recruitment Installation Accounting HealthCheck/Follow-up);
  my @queues = qw(Enrollment/Recruitment Installation Accounting HealthCheck/Follow-up);
  my $queue = $ticket-&gt;QueueObj-&gt;Name;
  my $queue = $ticket-&gt;QueueObj-&gt;Name;
  my $qCount = grep(/\Q$queue\E/,@queues);
  my $qCount = grep(/\Q$queue\E/,@queues);
  return undef unless $qCount &gt;= 1;
  return undef unless $qCount &gt;= 1;
 
 
  $RT::Logger-&gt;info('Merging into an allowed Queue');
  $RT::Logger-&gt;info('Merging into an allowed Queue');
 
 
  return 1;
  return 1;
 
 
  </nowiki>
  </nowiki>


=== Custom Action Preperation Code ===
=== Custom Action Preperation Code ===


  <nowiki>#nothing to do here.. just return
  <nowiki>#nothing to do here.. just return
return 1;
  return 1;
</nowiki>
  </nowiki>


=== Custom Action Cleanup Code ===
=== Custom Action Cleanup Code ===


  <nowiki> #Define the Custom Field Name Were Going to Play with.
  <nowiki> #Define the Custom Field Name Were Going to Play with.
  my $CFName = 'Lead Source';
  my $CFName = 'Lead Source';
 
  #Transaction Association
  #Transaction Association
  my $txnObj = $self-&gt;TransactionObj;
  my $txnObj = $self-&gt;TransactionObj;
 
  #Ticket Association
  #Ticket Association
  #The New Ticket your Merging into
  #The New Ticket your Merging into
  my $ticketObj  = $self-&gt;TicketObj;
  my $ticketObj  = $self-&gt;TicketObj;
  my $queueObj  = $self-&gt;TicketObj-&gt;QueueObj;
  my $queueObj  = $self-&gt;TicketObj-&gt;QueueObj;
  my $CFObj      = RT::CustomField-&gt;new($RT::SystemUser);
  my $CFObj      = RT::CustomField-&gt;new($RT::SystemUser);
    $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue =&gt; $queueObj-&gt;id);
      $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue =&gt; $queueObj-&gt;id);
unless($CFObj-&gt;id) {
  unless($CFObj-&gt;id) {
      $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue=&gt;0);
      $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue=&gt;0);
      unless($CFObj-&gt;id){
      unless($CFObj-&gt;id){
        $RT::Logger-&gt;warning("Custom Field: $CFName not for this Queue");
          $RT::Logger-&gt;warning("Custom Field: $CFName not for this Queue");
        return undef;
          return undef;
      };
      };
};
  };
 
#The old Ticket your merging From
  #The old Ticket your merging From
my $oldTicket = RT::Ticket-&gt;new($RT::SystemUser);
  my $oldTicket = RT::Ticket-&gt;new($RT::SystemUser);
$oldTicket-&gt;LoadById($txnObj-&gt;ObjectId);
  $oldTicket-&gt;LoadById($txnObj-&gt;ObjectId);
 
#We dont care if we blank out the Ticket Being Merged into.
  #skip merge into same ticket
my $cfv = $oldTicket-&gt;FirstCustomFieldValue($CFName);
  return undef if $oldTicket->id() == $ticketObj->id();
 
#ok lets set this..
  #Extract the fields (including multifields) from both tickets
  my @cfv1 = sort(uniq(split(/\n/, $oldTicket->CustomFieldValuesAsString($CFName))));
my ($st, $msg) = $ticketObj-&gt;AddCustomFieldValue(
  my @cfv2 = split(/\n/, $ticketObj->CustomFieldValuesAsString($CFName));
                                           Field =&gt; $CFObj-&gt;id,
 
                                           Value =&gt; $cfv,
  #Merge in the fields from the old ticket into the new ticket
                                           RecordTransaction =&gt; 1
  my $cfv = "";
  foreach $cfv (@cfv1)
  {
  if(! grep { $_ eq $cfv} @cfv2 )
  {
#$RT::Logger->warning("cfv: adding ". $cfv);
  my ($st, $msg) = $ticketObj->AddCustomFieldValue(
                                           Field => $CFObj->id,
                                           Value => $cfv,
                                           RecordTransaction => 0
                   );
                   );
   
   
unless ($st){
  unless ($st){
    $RT::Logger-&gt;warning("Odd we couldn't set $CFName to $cfv");
      $RT::Logger->warning("Odd we couldn't set $CFName to $cfv");
};
    };
return 1;
  }
</nowiki>
  }
 
  return 1;
  </nowiki>
[[Category:Alternate Custom Action Cleanup Code for Multiple Entries]]
[[Category:Alternate Custom Action Cleanup Code for Multiple Entries]]

Revision as of 13:43, 28 July 2011

Return true if current transaction is merge action.

Custom condition code:

my $txn = $self->TransactionObj;
return undef unless $txn->Type =~ /^AddLink$/i;
return undef unless $txn->Field =~ /^MergedInto$/i;
return 1;

If you need to refer to the original ticket id in a template it is available as $Transaction->ObjectId.

Variation:

Another Variation that says, if we are merging from a queue into a set of queues, and data from the merging tickets Custom fields need to be populated into the ticket your merging into..

Description: OnMergeCF
Condition:   User Defined
Action:      User Defined
Template:    Global Template: Transaction
State:       TransactionCreate

Custom Condition

#Transaction Association
   my $txn = $self->TransactionObj;
   
   #Condition on Type
   return undef unless $txn->Type =~ /^AddLink$/i;
   return undef unless $txn->Field =~ /^MergedInto$/i;
   
   $RT::Logger->info('Merge is Occurring');
   
   #Ticket Association
   #The New Ticket your Merging into
   my $ticket    = $self->TicketObj;
   
   #The old Ticket your merging From
   my $oldTicket = RT::Ticket->new($RT::SystemUser);
      $oldTicket->LoadById($txn->ObjectId);
   
   $RT::Logger->info('Merging '.$txn->ObjectId.' into '.$ticket->Id);
   
   #Are we merging from the right queue?
   my $oldqueue = $oldTicket->Queue;
   return undef unless $oldqueue == 36;
   
   $RT::Logger->info('Merging From Lead Tracking Queue');
   
   #Are we merging into an allowed queue?
   my @queues = qw(Enrollment/Recruitment Installation Accounting HealthCheck/Follow-up);
   my $queue = $ticket->QueueObj->Name;
   my $qCount = grep(/\Q$queue\E/,@queues);
   return undef unless $qCount >= 1;
   
   $RT::Logger->info('Merging into an allowed Queue');
   
   return 1;
   
   

Custom Action Preperation Code

#nothing to do here.. just return
  return 1;
  

Custom Action Cleanup Code

 #Define the Custom Field Name Were Going to Play with.
   my $CFName = 'Lead Source';
  
   #Transaction Association
   my $txnObj = $self->TransactionObj;
  
   #Ticket Association
   #The New Ticket your Merging into
   my $ticketObj  = $self->TicketObj;
   my $queueObj   = $self->TicketObj->QueueObj;
   my $CFObj      = RT::CustomField->new($RT::SystemUser);
      $CFObj->LoadByNameAndQueue(Name => $CFName, Queue => $queueObj->id);
  unless($CFObj->id) {
       $CFObj->LoadByNameAndQueue(Name => $CFName, Queue=>0);
       unless($CFObj->id){
          $RT::Logger->warning("Custom Field: $CFName not for this Queue");
          return undef;
       };
  };
  
  #The old Ticket your merging From
  my $oldTicket = RT::Ticket->new($RT::SystemUser);
  $oldTicket->LoadById($txnObj->ObjectId);
  
  #skip merge into same ticket
  return undef if $oldTicket->id() == $ticketObj->id();

  #Extract the fields (including multifields) from both tickets
  my @cfv1 = sort(uniq(split(/\n/, $oldTicket->CustomFieldValuesAsString($CFName))));
  my @cfv2 = split(/\n/, $ticketObj->CustomFieldValuesAsString($CFName));
  
  #Merge in the fields from the old ticket into the new ticket
  my $cfv = "";
  foreach $cfv (@cfv1)
  { 
  if(! grep { $_ eq $cfv} @cfv2 ) 
  {
#$RT::Logger->warning("cfv: adding ". $cfv);
   my ($st, $msg) = $ticketObj->AddCustomFieldValue(
                                          Field => $CFObj->id,
                                          Value => $cfv,
                                          RecordTransaction => 0
                  );
 
   unless ($st){
      $RT::Logger->warning("Odd we couldn't set $CFName to $cfv");
    };
   }
  }

  return 1;