[rt-users] Adding custom field values from Perl script.

Jacob Helwig Jacob at buschs.com
Sat Dec 9 09:37:42 EST 2006


Certainly:

#! /usr/bin/perl

use strict;
use DBI;
use DBD::Pg qw(:pg_types);

my $dbname = "feedback";
my $dbuser = "rt-feedback";
my $dbpass = "********************";

# Location of RT3's libs
use lib ("/usr/share/request-tracker3.6/lib/",
"/usr/local/share/request-tracker3.6/lib/");
# Pull in the RT stuff
package RT;
use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent
loc);

# Clean out the environment
CleanEnv();

# Load the RT configuration
RT::LoadConfig();

# Initialise RT
RT::Init();

use RT::Queue;
use RT::Tickets;
use RT::Group;
use RT::Record;

use Data::Dumper;

my $queue = new RT::Queue($RT::SystemUser);
$queue->Load("Help Desk");

my $tickets = new RT::Tickets($RT::SystemUser);

$tickets->FromSQL("id = 1");

my $user = RT::User->new($RT::SystemUser);

my $dbh = DBI->connect("dbi:Pg:dbname=$dbname", $dbuser, $dbpass,
{AutoCommit => 0, Username => $dbuser, pg_server_prepare => 1});
my $sth = $dbh->prepare("SELECT new_survey(?,?,?) AS survey_id"); 
my $sth_auth = $dbh->prepare("SELECT authcode FROM survey WHERE id =
?");

while (my $ticket = $tickets->Next) {
	my $creator = RT::User->new($RT::SystemUser);
	my $owner = RT::User->new($RT::SystemUser);
	$creator->Load($ticket->Creator());
	$owner->Load($ticket->Owner());

	print "Ticket #" . $ticket->id() . ": " . $ticket->Subject() .
"\n";
	print "\tCreated by " . $creator->RealName() . " on " .
$ticket->Created() . " owner " . $owner->RealName() . "\n";

	print "\tCustom field \"Store Number\" value: ";
	my $values = $ticket->CustomFieldValues("Store Number");
	while (my $value = $values->Next) {
		print $value->Content . "\n";
	}

	print "\tCustom field \"Feedback\" values:";
	my $values = $ticket->CustomFieldValues("Feedback");
	my $values = $ticket->CustomFieldValues(7);
	while (my $value = $values->Next) {
		print " " . $value->Content;
	}
	print "\n";

	my ($requestors) = $ticket->Requestors;
	($requestors) = $requestors->MembersObj;
	print "\tFound " . $requestors->Count . " requestors:\n";
	while (my $requestor = $requestors->Next()) {
		$dbh->pg_savepoint("requestor_entry");
		my $realname;
		my ($address) =
$requestor->MemberObj->Object->EmailAddress;
		if ($address) {
			($realname) =
$requestor->MemberObj->Object->RealName;
			my ($a) = "\"$realname\" <$address>";
			print "\t\t" . $a . "\n";
		}
		$sth->execute($realname, $address, $ticket->id()) or
print "Error executing SQL: " . $sth->errstr . "\n";
		my @surveyid = $sth->fetchrow_array();

		print "\tAdding feedback form #" . $surveyid[0] . " for
requestor: " . $realname . "\n";

		$sth_auth->execute($surveyid[0]) or print "Error
executing SQL: " . $sth_auth->errstr . "\n";
		my @authcode = $sth_auth->fetchrow_array();
		print "\t\tAuth code: " . $authcode[0] . "\n";

		if ($ticket->AddCustomFieldValue({ Field => 7, Value =>
"" . $surveyid[0] . "" })) {
			print "\tOK!\n";
		} else {
			print "\tFAILED!\n";
			$dbh->pg_rollback_to("requestor_entry");
		}
		$dbh->commit;
		$dbh->pg_release("requestor_entry");
	}
	print "\tCustom field \"Feedback\" values:";
	my $values = $ticket->CustomFieldValues("Feedback");
	my $values = $ticket->CustomFieldValues(7);
	while (my $value = $values->Next) {
		print " " . $value->Content;
	}
	print "\n";
}

$dbh->commit;
#$dbh->rollback;

$dbh->disconnect;

# Disconnect before we finish off
$RT::Handle->Disconnect();

exit 0;

> -----Original Message-----
> From: Stephen Turner [mailto:sturner at MIT.EDU]
> Sent: Friday, December 08, 2006 10:26
> To: Jacob Helwig; rt-users at lists.bestpractical.com
> Subject: RE: [rt-users] Adding custom field values from Perl script.
> 
> 
> 	I'm trying to add some values to a few tickets after I've gotten
> the
> ticket object for them, without much luck.  I'm able to see any
> existing
> values, no problem, but running $ticket->AddCustomFieldValue({ Field
=>
> $field_id, Value => $field_value }) doesn't appear to be doing
anything
> other than returning 1.  Checking the custom field values before and
> after
> running AddCustomFieldValue shows no changes, as does showing the
> ticket
> through the web interface.  I know I'm using the right field id, since
> it's
> the same one I'm using to check what the current values are, and
> field_value
> shouldn't be a problem, since it's just an int.  Am I going about this
> entirely the wrong way?  Something obvious that I'm just not seeing,
> after
> having stared at this for so long?
> 
> ---------------------------------------------
> 
> Jacob, could you post the relevant section of your script?
> 
> Steve
> 
> ----------------------------------------
> Stephen Turner
> Senior Programmer/Analyst - Client Support Services
> MIT Information Services and Technology (IS&T)
> 
> 




More information about the rt-users mailing list