RT: Mass Custom Field Update

Robrt on 2005-07-17T07:25:16

Steve Peters asked if I could consolidate the Win32 and mswwin32 types in the Operating System CustomField in perlbug. Definitely a reasonable request, especially since we couldn't figure out what the difference was.

First try was to use RT's bulk update, but it didn't want to let me change custom fields... so... I wrote a script:

#!/home/perl/bin/perl
use strict;
use warnings;
use RT;
use RT::Queues;
use RT::Tickets;
RT::LoadConfig();
RT::Init();
my $tx = RT::Tickets->new($RT::SystemUser);
my $cf = RT::CustomField->new($RT::SystemUser);
my $q  = RT::Queue->new($RT::SystemUser);
$tx->FromSQL(q[queue="perl5" and "cf.perl5.{Operating System}" = "Win32"]);
$q->Load('perl5');
$cf->LoadByNameAndQueue(Queue => $q->Id,
                        Name => 'Operating System');
while (my $t = $tx->Next) {
    my $os = $t->FirstCustomFieldValue("Operating System");
    $t->DeleteCustomFieldValue(Field => $cf->Id,
                               Value => $os);
    $t->AddCustomFieldValue(Field => $cf->Id,
                            Value => 'mswin32');
}

It gets the job done. There's a few things that aren't quite "simple enough"... but we'll see about knocking those out in the next version.

Learn more about RT in the soon to be published book RT Essentials.


Thanks!

knowmad on 2007-12-20T17:07:45

Thanks for the code sample. I've revised it to be more configurable and output more error checking. The final result is in the RT Wiki at http://wiki.bestpractical.com/view/CustomField.