Setting Custom Field values in Actions/Scrips

I’ve been asked to calculate some new metrics in our RT system. To try to make things simple, I’m using Custom Fields to hold the values (Enter one value) to hold the calculated values. The fields are currently modifiable only by root, and viewable by the rest of the system. For testing, I have my action tied to “On Resolve Set XXX”. Using RT::Logger, I can see everything is working except setting the values. The best I’ve gotten is an error message:

is no longer a value for custom field TTWOO
TWOO changed to 0

I’ve been Googling and checking the forum and wiki but nothing is working. This is the last thing I tried: CopyContentToCF - Request Tracker Wiki

My code currently is:
my $twoo_obj - RT::CustomField->New($RT::SystemUser);
My $ttwoo_obj = RT::CustomField->new($RT::SystemUser);
$twoo_obj->Load(29);
$ttwoo_obj->Load(30);
$twoo_obj->AddValueForObject(Object => $ticket, Content => $twoo);
$ttwoo_obj->AddValueForObject(Object => $ticket, Content => $ttwoo);

What am I doing wrong? Any help would be appreciated.

I get the same error if I use the method outlined here: How do I set a Custom Field on ticket creation based on To address? - IS&T Contributions - Hermes

If you want to add CustomField value for ticket there is a simple way:

my ($status, $msg) = $self->TicketObj->AddCustomFieldValue( Field =>29, Value => $twoo);
$RT::Logger->error(“Couldn’t set CF FRP: $msg”) unless $status;
}

($status, $msg) = $self->TicketObj->AddCustomFieldValue( Field =>30, Value => $ttwoo);
$RT::Logger->error(“Couldn’t set CF FRP: $msg”) unless $status;
}

I believe that was the method described in the second link I posted, but I’ll give this a try once I wrap up my mail log audit. Thank you!

Nope. Still didn’t work. This is the weird part. Using the suggested code, I can see in my rt.log file:
[27203] [Mon Oct 23 12:38:11 2017] [debug]: 1: Status: 23315 Msg: TWOO 0 changed to 3.8 (/opt/rt4/sbin/…/local/lib/RT/Action/SetTWOO.pm:121)
[27203] [Mon Oct 23 12:38:11 2017] [debug]: 2: Status: 23316 Msg: TTWOO 4.81 added (/opt/rt4/sbin/…/local/lib/RT/Action/SetTWOO.pm:123)

However, when the web page loads, I get:
Ticket 13195: Status changed from ‘stalled’ to 'new’
4.81 is no longer a value for custom field TTWOO
TWOO 3.8 changed to 0

To my knowledge, I don’t have any other Scrips running that would cause a Custom Field value to be set/reset. I am using a CF type of “Enter one value”. Should I be using a different one? Do I need to convert the numbers to a string first? I’m kind of scratching my head here.

This is the full code for the Action I’m performing.

package RT::Action::SetTWOO;
use strict;
use warnings;
use base ‘RT::Action’;
use Math::Round;

sub Prepare {

Nothing to prepare

return 1;
}

sub Commit {
my $self = shift;
#=========================================================================================
my $ticket = $self->TicketObj; # Get the current ticket
my $transactions = $ticket->Transactions; # Get the ticket transaction list
#=========================================================================================
my $start_date = RT::Date->new($RT::SystemtUser); # Define the date that “Waiting on ourselves” was set
my $end_date = RT::Date->new($RT::SystemUser); # Define the date that ‘ends’ WOO
my $calc = 0; # 0 = false, 1 = true
my $no_end = 1; # 0 = false, 1 = true
my $twoo = 0; # Time Waiting On Ourselves
my $ttwoo = 0; # Total Time Waiting On Ourselves
my $convert = 86400; # Number of seconds in a day
#=========================================================================================
my $set_WOO = “changed to Ourselves”; # State has moved to Ourselves
my $waiting_on = “Waiting On”; # “Trigger string” to test on
my $reset_WOO = “Waiting On Ourselves changed to”; #State has been cleared from Ourselves
my $add_WOO = “Waiting On Ourselves added”; # Value was set for ticket, not automatically
my $delete_WOO = “Waiting On Ourselves deleted”; # CF was blanked out
#=========================================================================================

Limit the types of transactions we are intersted in

$transactions->Limit(FIELD => ‘Type’, OPERATOR => ‘LIKE’, VALUE => ‘%CustomField%’);
$transactions->Limit(ENTRYAGGREGATOR => “OR”, FIELD => ‘Type’, OPERATOR => ‘LIKE’, VALUE => ‘%Create%’);

$RT::Logger->debug("*** Current user is: " . $RT::SystemUser);
$RT::Logger->debug("*** BEGIN TXN DUMP");
while (my $trn = $transactions->Next)
{
# If the entry is the ticket being created
if (index($trn->Type,“Create”) != -1)
{
$start_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$calc = 0;
$no_end = 1;
$RT::Logger->debug("*** WE MADE A TICKET: " . $start_date);
}
# If the custom field was “Waiting On"
elsif (index($trn->Description,$waiting_on) != -1)
{
# If the state left WOO
if (index($trn->Description,$reset_WOO) != -1)
{
$end_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$calc = 1;
$no_end = 0;
$RT::Logger->debug(”*** NOT OUR PROBLEM ANYMORE: " . $end_date);
}
# If the state changed to WOO
elsif (index($trn->Description,$set_WOO) != -1)
{
$RT::Logger->debug("*** WAITING ON OUTSELVES");
$start_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$calc = 0;
$no_end = 1;
}
# If the state was manually set from an unknown state
elsif (index($trn->Description,$add_WOO) != -1)
{
$RT::Logger->debug("*** SOMEONE FORGOT TO SET THE FIELD");
$start_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$no_end = 1;
$calc = 0;
}
elsif (index($trn->Description,$delete_WOO) != -1)
{
$RT::Logger->debug("*** WOO DELETED");
$end_date->Set(Format => ‘ISO’, Value => $trn->Created, Timezone => ‘user’);
$no_end = 0;
$calc = 1;
}
# Some other transition for Waitin On
else {
$RT::Logger->debug("*** WAITING ON IS SET TO SOMETHING ELSE");
$calc = 0;
}
# If we have an end date, calculate the time spent and add it to the total
if ($calc == 1)
{
my $temp_time = $end_date->Diff($start_date);
$ttwoo = $ttwoo + $temp_time;
$RT::Logger->debug("*** We spent " . $temp_time . " sec waiting on ourselves.");
}
}
}

If we made it this far with no end date, then we need to use the current date to calucate time

if ($no_end == 1)
{
$end_date->SetToNow;
$twoo = $end_date->Diff($start_date);
$ttwoo = $ttwoo + $twoo;
$RT::Logger->debug(“TWOO: " . $twoo . " in seconds”);
$RT::Logger->debug(“TTWOO: " . $ttwoo . " in seconds”);

$twoo = nearest(.01,$twoo/$convert);
$ttwoo = nearest(.01,$ttwoo/$convert);

}
$RT::Logger->debug(“TWOO: " . $twoo . " in days”);
$RT::Logger->debug(“TTWOO: " . $ttwoo . " in days”);
my ($status,$msg) = $ticket->AddCustomFieldValue(Field => 29, Value => $twoo);
$RT::Logger->debug(“1: Status: $status Msg: $msg”);
($status, $msg) = $ticket->AddCustomFieldValue(Field => 30, Value => $ttwoo);
$RT::Logger->debug(“2: Status: $status Msg: $msg”);
$RT::Logger->debug("*** TXN HISTORY ENDS ***");
}

1;

Here’s something else I noticed… In the Ticket history, I can see:

The RT System Itself - TWOO 0 changed to 3.99

However, just BELOW it I see:
root (Enoch Root) - TWOO 3.99 changed to 0

So it IS making the change, but the question is why is “root” changing it back? I am logged in as root when I’m testing the Scrip via the browser.

Even as a non-root user with an account (that I believe) doesn’t have permissions to modify the CustomField, it still resets it. RT System sets the correct value, user account resets it. Curiouser and Curiouser…

I am currently testing it using a status change as the trigger for simplicity. So, I have a Scrip: On Status Change, Set Time Waiting On Ourselves. Is that somehow causing the web page to rewrite the values?

How about using Transaction Batch?

Regards,

Joop

That was it! I’ve never had to change that before, so I didn’t even think to look there. Now, this won’t have any bearing when I migrate this to running via rt-crontool correct? I can just write the search corectly & leave the action as my custom Action & it should work just fine?

Also, I’m storing numbers in the custom field (as you can see). Is there a way in the Query builder/etc to make it treat the field as a number? I’m able to search the field but the < & > tests are not working.

Another user has asked this same question: Numeric custom field values

Can’t say much about RT, but in SQL, you can usually fix this by forcing a conversion. For example:
old: ‘CF.{Helpdesk Priority}’ > 15
new: (0 + ‘CF.{Helpdesk Priority}’) > 15
You might be able to do this with the RT search.

RT doesn’t like that. I get this:

Wrong query, expecting a OPERATOR in ‘(0+>‘CF.{Time Waiting On Ourselves (days)}’<–here) > 10’

when I use this in “Advanced”: (0+‘CF.{Time Waiting On Ourselves (days)}’) > 10

Add more spaces?
ex: ( 0 + ‘CF.{Time Waiting On Ourselves (days)}’ ) > 10

Sadly no. Did a copy/paste right from the post and I still get the error.

Any news on this.

I have th esameproblem with my scrips.