OnMerge: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
(Changed code to handle multiple value fields and to actually merge, not overwrite)
mNo edit summary
 
(2 intermediate revisions by 2 users not shown)
Line 22: Line 22:
=== Custom Condition ===
=== Custom Condition ===


  <nowiki>#Transaction Association
  <pre>
  my $txn = $self-&gt;TransactionObj;
    #Transaction Association
    my $txn = $self-&gt;TransactionObj;
   
    #Condition on Type
    return undef unless $txn-&gt;Type =~ /^AddLink$/i;
    return undef unless $txn-&gt;Field =~ /^MergedInto$/i;
   
    $RT::Logger-&gt;info('Merge is Occurring');
   
    #Ticket Association
    #The New Ticket your Merging into
    my $ticket    = $self-&gt;TicketObj;
   
    #The old Ticket your merging From
    my $oldTicket = RT::Ticket-&gt;new($RT::SystemUser);
      $oldTicket-&gt;LoadById($txn-&gt;ObjectId);
   
    $RT::Logger-&gt;info('Merging '.$txn-&gt;ObjectId.' into '.$ticket-&gt;Id);
   
    #Are we merging from the right queue?
    my $oldqueue = $oldTicket-&gt;Queue;
    return undef unless $oldqueue == 36;
   
    $RT::Logger-&gt;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-&gt;QueueObj-&gt;Name;
    my $qCount = grep(/\Q$queue\E/,@queues);
    return undef unless $qCount &gt;= 1;
   
    $RT::Logger-&gt;info('Merging into an allowed Queue');
   
    return 1;
   
    </pre>
 
=== Custom Action Preperation Code ===
 
<pre>
  #nothing to do here.. just return
  return 1;
</pre>
 
=== Custom Action Cleanup Code ===
 
<pre>
    use List::MoreUtils qw/ uniq /;
 
    #Define the Custom Field Name Were Going to Play with.
    my $CFName = 'Lead Source';
    
    
  #Condition on Type
    #Transaction Association
  return undef unless $txn-&gt;Type =~ /^AddLink$/i;
    my $txnObj = $self-&gt;TransactionObj;
  return undef unless $txn-&gt;Field =~ /^MergedInto$/i;
    
    
  $RT::Logger-&gt;info('Merge is Occurring');
    #Ticket Association
 
    #The New Ticket your Merging into
   #Ticket Association
    my $ticketObj  = $self-&gt;TicketObj;
  #The New Ticket your Merging into
    my $queueObj  = $self-&gt;TicketObj-&gt;QueueObj;
  my $ticket    = $self-&gt;TicketObj;
    my $CFObj      = RT::CustomField-&gt;new($RT::SystemUser);
      $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue =&gt; $queueObj-&gt;id);
   unless($CFObj-&gt;id) {
        $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue=&gt;0);
        unless($CFObj-&gt;id){
          $RT::Logger-&gt;warning("Custom Field: $CFName not for this Queue");
          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($txn-&gt;ObjectId);
  $oldTicket-&gt;LoadById($txnObj-&gt;ObjectId);
    
    
   $RT::Logger-&gt;info('Merging '.$txn-&gt;ObjectId.' into '.$ticket-&gt;Id);
   #skip merge into same ticket
    
   return undef if $oldTicket->id() == $ticketObj->id();
  #Are we merging from the right queue?
  my $oldqueue = $oldTicket-&gt;Queue;
   #Extract the fields (including multifields) from both tickets
  return undef unless $oldqueue == 36;
   my @cfv1 = sort(uniq(split(/\n/, $oldTicket->CustomFieldValuesAsString($CFName))));
 
   my @cfv2 = split(/\n/, $ticketObj->CustomFieldValuesAsString($CFName));
  $RT::Logger-&gt;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-&gt;QueueObj-&gt;Name;
  my $qCount = grep(/\Q$queue\E/,@queues);
  return undef unless $qCount &gt;= 1;
 
  $RT::Logger-&gt;info('Merging into an allowed Queue');
 
  return 1;
    
    
   </nowiki>
   #Merge in the fields from the old ticket into the new ticket
 
  my $cfv = "";
=== Custom Action Preperation Code ===
  foreach $cfv (@cfv1)
 
  {
  <nowiki>#nothing to do here.. just return
  if(! grep { $_ eq $cfv} @cfv2 )
  return 1;
  {
  </nowiki>
  #$RT::Logger->warning("cfv: adding ". $cfv);
 
    my ($st, $msg) = $ticketObj->AddCustomFieldValue(
=== Custom Action Cleanup Code ===
                                          Field => $CFObj->id,
 
                                          Value => $cfv,
<nowiki> #Define the Custom Field Name Were Going to Play with.
                                          RecordTransaction => 0
  my $CFName = 'Lead Source';
                  );
    
    
  #Transaction Association
    unless ($st){
  my $txnObj = $self-&gt;TransactionObj;
       $RT::Logger->warning("Odd we couldn't set $CFName to $cfv");
 
    };
  #Ticket Association
    }
  #The New Ticket your Merging into
  }
  my $ticketObj  = $self-&gt;TicketObj;
  my $queueObj  = $self-&gt;TicketObj-&gt;QueueObj;
  my $CFObj      = RT::CustomField-&gt;new($RT::SystemUser);
      $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue =&gt; $queueObj-&gt;id);
  unless($CFObj-&gt;id) {
       $CFObj-&gt;LoadByNameAndQueue(Name =&gt; $CFName, Queue=&gt;0);
      unless($CFObj-&gt;id){
          $RT::Logger-&gt;warning("Custom Field: $CFName not for this Queue");
          return undef;
      };
  };
 
  #The old Ticket your merging From
  my $oldTicket = RT::Ticket-&gt;new($RT::SystemUser);
  $oldTicket-&gt;LoadById($txnObj-&gt;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){
   return 1;
      $RT::Logger->warning("Odd we couldn't set $CFName to $cfv");
</pre>
    };
  }
  }


  return 1;
  </nowiki>
[[Category:Alternate Custom Action Cleanup Code for Multiple Entries]]
[[Category:Alternate Custom Action Cleanup Code for Multiple Entries]]

Latest revision as of 17:51, 13 August 2016

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

    use List::MoreUtils qw/ uniq /;

    #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;