OnMerge

From Request Tracker Wiki
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

    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;