[rt-users] RTAT Import?
Jean-Sebastien Morisset
jsmoriss at mvlan.net
Wed Apr 30 13:51:39 EDT 2008
On Wed, Apr 30, 2008 at 01:06:55PM -0400, Matthew Keller wrote:
> If you look in the API documentation about setting a CF (the
> 'CustomField' object), it will show you where my oversimplification of
> $cf->Value=$thing was in err, and how to do it correctly.
Aha! Thanks! Ok, so far this seems to work well:
---BEGIN---
#!/usr/bin/perl
use lib qw(/opt/rt3/local/lib /opt/rt3/lib);
use RT;
use RTx::AssetTracker::Asset;
RT::LoadConfig();
RT::Init();
use strict;
if (! $ARGV[0] || $ARGV[0] =~ /^--?h/) {
print "Syntax: $0 {csvfile}\n";
exit 0;
}
open(CSV, "< $ARGV[0]") or die "$!\n";
my $at = RTx::AssetTracker::Asset->new(RT->SystemUser);
my $line;
# customfield columns in csv file
my %cf_map = (
'Client' => 4,
'Location' => 5,
'Manufacturer' => 6,
'Model' => 7,
'Serial Number' => 8,
'OS Name' => 9,
);
while (<CSV>) {
chomp; $line++;
next if ($line == 1); # Skip CSV Header
s/^"//; s/"$//;
my @csv = split(/"?,"?/);
print $csv[0], ":\n";
unless (my $id = $at->Load($csv[0])) {
my ($id, $t, $msg) = $at->Create (
Name => $csv[0],
Status => $csv[1],
Type => $csv[2],
Description => $csv[3],
);
print "\t$msg\n" if ($msg);
}
my $atcf = $at->CustomFields();
while (my $cf = $atcf->Next()) {
# check to see if we have a CSV column for this customfield
if (my $col = $cf_map{$cf->Name}) {
my ($status, $msg) = $at->AddCustomFieldValue(
Field => $cf->Id, Value => $csv[$col]);
print "\t$msg\n" if ($msg);
}
}
}
---END---
And for reference, here's the CSV file I'm using:
"NAME","STATUS","TYPE","DESCRIPTION","CLIENT","LOCATION","MANUFACTURER","MODEL","SERIAL NUMBER","OS NAME"
Column 5 (CLIENT) and up are all custom fields and must be defined in
the %cf_map hash. If anyone wants to make this a little more elegant,
feel free. :-)
js.
--
Jean-Sebastien Morisset, Sr. UNIX Administrator <jsmoriss at mvlan.net>
More information about the rt-users
mailing list