Difference between revisions of "ImportCustomFieldValues"

From Request Tracker Wiki
Jump to navigation Jump to search
 
 
(3 intermediate revisions by 2 users not shown)
Line 3: Line 3:
I use this script to populate a "select one from many" custom field from a text file generated from a database query.
I use this script to populate a "select one from many" custom field from a text file generated from a database query.


  <nowiki>#!/usr/bin/perl
  <pre>   #!/usr/bin/perl
 
 
  use warnings;
  use warnings;
  use strict;
  use strict;
 
 
  use lib qw(/opt/rt3/local/lib /opt/rt3/lib);
  use lib qw(/opt/rt3/local/lib /opt/rt3/lib);
  use Getopt::Long;
  use Getopt::Long;
 
 
  use RT;
  use RT;
  RT::LoadConfig();
  RT::LoadConfig();
  RT::Init();
  RT::Init();
 
 
  my %args;
  my %args;
 
 
  GetOptions(\%args, 'field=s', 'update','replace' ,'help', 'verbose');
  GetOptions(\%args, 'field=s', 'update','replace' ,'help', 'verbose');
 
 
 
 
  if ($args{'help'} || !($args{'update'}||$args{'replace'})  ){
    if ($args{'help'} || !($args{'update'}||$args{'replace'})  ){
    help();
      help();
    exit;
      exit;
    }
 
  my @lines = &lt;STDIN&gt;;
 
  map {chomp} @lines;
 
  my $cf = RT::CustomField-&gt;new(RT-&gt;SystemUser);
 
  $cf-&gt;Load( $args{'field'} );
  unless ( $cf-&gt;id ) {
      die "Couldn't find that custom field\n";
   }
   }
 
 
  my @lines = &lt;STDIN&gt;;
  if ( $args{'replace'} ) {
 
      my $values = $cf-&gt;Values;
  map {chomp} @lines;
      my %map;
 
 
  my $cf = RT::CustomField-&gt;new(RT-&gt;SystemUser);
      while ( my $value = $values-&gt;Next ) {
 
          unless (grep {$value-&gt;Name} @lines) {
  $cf-&gt;Load( $args{'field'} );
          print STDERR "Deleting " . $value-&gt;Name . "\n" if ($args{'verbose'});
  unless ( $cf-&gt;id ) {
              $value-&gt;Delete();
    die "Couldn't find that custom field\n";
          }
  }
 
 
      }
  if ( $args{'replace'} ) {
  }
    my $values = $cf-&gt;Values;
 
    my %map;
  if ( $args{'update'} || $args{'replace'} ) {
 
      my $values = $cf-&gt;Values;
    while ( my $value = $values-&gt;Next ) {
      my @current;
        unless (grep {$value-&gt;Name} @lines) {
 
          print STDERR "Deleting " . $value-&gt;Name . "\n" if ($args{'verbose'});
      while ( my $value = $values-&gt;Next ) {
            $value-&gt;Delete();
 
        }
          push @current, $value-&gt;Name;
 
      }
    }
 
  }
      foreach my $entry (@lines) {
 
          unless ( grep { $entry eq $_ } @current ) {
  if ( $args{'update'} || $args{'replace'} ) {
              print STDERR "Adding " . $entry . "\n" if ($args{'verbose'});
    my $values = $cf-&gt;Values;
              my ( $ret, $val ) = $cf-&gt;AddValue( Name =&gt; $entry );
    my @current;
          }
 
      }
    while ( my $value = $values-&gt;Next ) {
 
 
  }
        push @current, $value-&gt;Name;
 
    }
  print STDERR "Done\n" if ($args{'verbose'});
 
 
    foreach my $entry (@lines) {
 
        unless ( grep { $entry eq $_ } @current ) {
 
            print STDERR "Adding " . $entry . "\n" if ($args{'verbose'});
  sub help {
            my ( $ret, $val ) = $cf-&gt;AddValue( Name =&gt; $entry );
 
        }
  print &lt;&lt;EOF
    }
 
 
  $0 is a simple RT tool to add values to a custom field.
  }
  It takes several arguments:
 
 
  print STDERR "Done\n" if ($args{'verbose'});
 
 
    --field    The id or name of the custom field you'd like to work with
 
    --update  Add values to this field, but do not prune unused files
 
    --replace  Make the custom field contain only the values listed
  sub help {
    --verbose  Show progress messages
 
 
  print &lt;&lt;EOF
  This script expects a list of potential custom field values,
 
  one per line to be fed to STDIN.
  $0 is a simple RT tool to add values to a custom field.
 
  It takes several arguments:
  Example:
 
 
 
  $0 --field "My CF Name" --update &lt; list_of_values
  --field    The id or name of the custom field you'd like to work with
 
  --update  Add values to this field, but do not prune unused files
  EOF
  --replace  Make the custom field contain only the values listed
 
 
  }
 
  </pre>
  This script expects a list of potential custom field values,
  one per line to be fed to STDIN.
 
  Example:
 
  $0 --field "My CF Name" --update &lt; list_of_values
 
  EOF
 
  }
  </nowiki>


The replace switch seems to update on version 3.8.7. (SJN)
The replace switch seems to update on version 3.8.7. (SJN)

Latest revision as of 12:15, 6 August 2016

import-custom-field-values

I use this script to populate a "select one from many" custom field from a text file generated from a database query.

   #!/usr/bin/perl
   
   use warnings;
   use strict;
   
   use lib qw(/opt/rt3/local/lib /opt/rt3/lib);
   use Getopt::Long;
   
   use RT;
   RT::LoadConfig();
   RT::Init();
   
   my %args;
   
   GetOptions(\%args, 'field=s', 'update','replace' ,'help', 'verbose');
   
   
    if ($args{'help'} || !($args{'update'}||$args{'replace'})  ){
      help();
      exit;
    }
   
   my @lines = <STDIN>;
   
   map {chomp} @lines;
   
   my $cf = RT::CustomField->new(RT->SystemUser);
   
   $cf->Load( $args{'field'} );
   unless ( $cf->id ) {
      die "Couldn't find that custom field\n";
   }
   
   if ( $args{'replace'} ) {
      my $values = $cf->Values;
      my %map;
   
      while ( my $value = $values->Next ) {
          unless (grep {$value->Name} @lines) {
           print STDERR "Deleting " . $value->Name . "\n" if ($args{'verbose'});
              $value->Delete();
          }
   
      }
   }
   
   if ( $args{'update'} || $args{'replace'} ) {
      my $values = $cf->Values;
      my @current;
   
      while ( my $value = $values->Next ) {
   
          push @current, $value->Name;
      }
   
      foreach my $entry (@lines) {
          unless ( grep { $entry eq $_ } @current ) {
              print STDERR "Adding " . $entry . "\n" if ($args{'verbose'});
              my ( $ret, $val ) = $cf->AddValue( Name => $entry );
          }
      }
   
   }
   
   print STDERR "Done\n" if ($args{'verbose'});
   
   
   
   sub help {
   
   print <<EOF
   
   $0 is a simple RT tool to add values to a custom field.
   It takes several arguments:
   
   
    --field    The id or name of the custom field you'd like to work with
    --update   Add values to this field, but do not prune unused files
    --replace  Make the custom field contain only the values listed
    --verbose  Show progress messages
   
   This script expects a list of potential custom field values,
   one per line to be fed to STDIN.
   
   Example:
   
   $0 --field "My CF Name" --update < list_of_values
   
   EOF
   
   }
   

The replace switch seems to update on version 3.8.7. (SJN)