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?

Thanks,

Jacob

Jacob Helwig

PC Technician

Busch’s Help Desk

Desk: x35221

Direct: 734-214-8221

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)

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@MIT.EDU]
Sent: Friday, December 08, 2006 10:26
To: Jacob Helwig; rt-users@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

-----Original Message-----
From: rt-users-bounces@lists.bestpractical.com
[mailto:rt-users-bounces@lists.bestpractical.com] On Behalf
Of Jacob Helwig
Sent: Saturday, December 09, 2006 9:38 AM
To: rt-users@lists.bestpractical.com
Subject: RE: [rt-users] Adding custom field values from Perl script.

Certainly:

  if ($ticket->AddCustomFieldValue({ Field => 7, Value =>

“” . $surveyid[0] . “” })) {
print “\tOK!\n”;
} else {
print “\tFAILED!\n”;
$dbh->pg_rollback_to(“requestor_entry”);
}

Jacob,

I can’t see anything obviously wrong with the AddCustomFieldValue statement

  • I’d recommend using the Perl debugger to step into the AddCustomFieldValue
    method (in Record.pm) and see what it does.

I’m wondering about the use of the transaction control on the $dbh handle
though. The only database updates in the script are done through the RT API
using a separate database handle ($RT::Handle) - so the rollbacks, commits,
etc that you do on $dbh have no effect on these updates. The RT API has
methods to control transactions, so it might be better to use those.

Steve

That $dbh is for a separate database for storing feedback “forms” sent
out to people that have used our help desk. Any changes to the RT
database, I am doing through the RT interface. my $sth =
$dbh->prepare(“SELECT * FROM new_survey(?,?,?)”); actually inserts into
the feedback database, and returns the id of the inserted row, updating
a few other tables in that database, as necessary.

I will take a closer look at AddCustomFieldValue, though.

-----Original Message-----
From: Stephen Turner [mailto:sturner@MIT.EDU]
Sent: Monday, December 11, 2006 10:04
To: Jacob Helwig; rt-users@lists.bestpractical.com
Subject: RE: [rt-users] Adding custom field values from Perl script.

Jacob,

I can’t see anything obviously wrong with the AddCustomFieldValue
statement

  • I’d recommend using the Perl debugger to step into the
    AddCustomFieldValue
    method (in Record.pm) and see what it does.

I’m wondering about the use of the transaction control on the $dbh
handle
though. The only database updates in the script are done through the
RT
API
using a separate database handle ($RT::Handle) - so the rollbacks,
commits,
etc that you do on $dbh have no effect on these updates. The RT API
has

That $dbh is for a separate database for storing feedback “forms” sent
out to people that have used our help desk. Any changes to the RT
database, I am doing through the RT interface. my $sth =
$dbh->prepare(“SELECT * FROM new_survey(?,?,?)”); actually
inserts into
the feedback database, and returns the id of the inserted
row, updating
a few other tables in that database, as necessary.

Ah, got it - I just saw the SELECT and just assumed no updates.

Steve

Found the error. I had an unnessicary set of curly braces around the
params for AddCustomFieldValue.

Changed:

$ticket->AddCustomFieldValue({ Field => 7, Value => $surveyid[0] })

To:

$ticket->AddCustomFieldValue( Field => 7, Value => $surveyid[0] )

Now it’s all working.