Difference between revisions of "OnMerge"

From Request Tracker Wiki
Jump to navigation Jump to search
 
m
 
(4 intermediate revisions by 3 users not shown)
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 23: 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;
    #Condition on Type
  return undef unless $txn-&gt;Field =~ /^MergedInto$/i;
    return undef unless $txn-&gt;Type =~ /^AddLink$/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
   
  #The New Ticket your Merging into
    #Ticket Association
  my $ticket    = $self-&gt;TicketObj;
    #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);
    #The old Ticket your merging From
    $oldTicket-&gt;LoadById($txn-&gt;ObjectId);
    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);
   
 
    $RT::Logger-&gt;info('Merging '.$txn-&gt;ObjectId.' into '.$ticket-&gt;Id);
  #Are we merging from the right queue?
   
  my $oldqueue = $oldTicket-&gt;Queue;
    #Are we merging from the right queue?
  return undef unless $oldqueue == 36;
    my $oldqueue = $oldTicket-&gt;Queue;
 
    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?
   
  my @queues = qw(Enrollment/Recruitment Installation Accounting HealthCheck/Follow-up);
    #Are we merging into an allowed queue?
  my $queue = $ticket-&gt;QueueObj-&gt;Name;
    my @queues = qw(Enrollment/Recruitment Installation Accounting HealthCheck/Follow-up);
  my $qCount = grep(/\Q$queue\E/,@queues);
    my $queue = $ticket-&gt;QueueObj-&gt;Name;
  return undef unless $qCount &gt;= 1;
    my $qCount = grep(/\Q$queue\E/,@queues);
 
    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>
   
    </pre>


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


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


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


<nowiki> #Define the Custom Field Name Were Going to Play with.
<pre>
  my $CFName = 'Lead Source';
    use List::MoreUtils qw/ uniq /;
 
    #Define the Custom Field Name Were Going to Play with.
    my $CFName = 'Lead Source';
 
    #Transaction Association
    my $txnObj = $self-&gt;TransactionObj;
 
    #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();
   
   
  #Transaction Association
  #Extract the fields (including multifields) from both tickets
  my $txnObj = $self-&gt;TransactionObj;
  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");
    };
    }
  }
   
   
  #Ticket Association
  return 1;
  #The New Ticket your Merging into
</pre>
  my $ticketObj  = $self-&gt;TicketObj;
 
  my $queueObj  = $self-&gt;TicketObj-&gt;QueueObj;
[[Category:Alternate Custom Action Cleanup Code for Multiple Entries]]
  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);
#We dont care if we blank out the Ticket Being Merged into.
my $cfv = $oldTicket-&gt;FirstCustomFieldValue($CFName);
#ok lets set this..
my ($st, $msg) = $ticketObj-&gt;AddCustomFieldValue(
                                          Field =&gt; $CFObj-&gt;id,
                                          Value =&gt; $cfv,
                                          RecordTransaction =&gt; 1
                  );
unless ($st){
    $RT::Logger-&gt;warning("Odd we couldn't set $CFName to $cfv");
};
return 1;
</nowiki>

Latest revision as of 18: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;