OnMerge RT CF Merging

Looking around on how to merge CF with multiple entries in RT, I couldn’t find anything and this was only useful as a starting point.  I have to admit I couldn’t figure out how to add a section to the RT Wiki, so I’m leaving the code here for future reference.

Here’s what replaces the Custom Action Cleanup Code:

#Define the Custom Field Name Were Going to Play with.
my $CFName = 'Auction';

#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 you're merging From
my $oldTicket = RT::Ticket->new($RT::SystemUser);
$oldTicket->LoadById($txnObj->ObjectId);

my $field = $ticketObj->CustomFieldValues($CFObj->id);
my @currentvals;
while (my $value = $field->Next) {
 push @currentvals, $value->Content;
}

my $oldfield = $oldTicket->CustomFieldValues($CFObj->id);
unless ($oldfield) {
 $RT::Logger->warning("No entries for $CFName to merge");
 return undef;
}
while (my $value = $oldfield->Next) {
 my $content = $value->Content;
 if (grep {/^$content$/} @currentvals) {
 $RT::Logger->warning("Already found $content in $CFName");
 next;
 }
 my ($st, $msg) = $ticketObj->AddCustomFieldValue(
 Field => $CFObj->id,
 Value => $content,
 RecordTransaction => 1
 );

 if ($st){
 $RT::Logger->warning("Added $content to $CFName");
 } else {
 $RT::Logger->warning("Odd we couldn't set $CFName to $content");
 }
}

 return 1;
Posted in Code by Matt at December 29th, 2010.

Comments are closed.