OnMerge

From Request Tracker Wiki
Revision as of 19:48, 29 December 2010 by 38.99.213.170 (talk) (→‎Custom Condition)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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);
 
 #We dont care if we blank out the Ticket Being Merged into.
 my $cfv = $oldTicket->FirstCustomFieldValue($CFName);
 
 #ok lets set this..
 
 my ($st, $msg) = $ticketObj->AddCustomFieldValue(
                                          Field => $CFObj->id,
                                          Value => $cfv,
                                          RecordTransaction => 1
                  );
 
 unless ($st){
     $RT::Logger->warning("Odd we couldn't set $CFName to $cfv");
 };
 return 1;