[rt-users] Append Data to CustomFields

Michael Coakley mike.coakley at gmail.com
Mon Jun 4 20:54:28 EDT 2012


I didn't see anyone else reply so here is some template code (very wordy to be clear) that should get you going...

Custom Condition
 
# Get the transaction object
my $TransObj = $self->TransactionObj;
 
# Make sure the transaction is a Comment before we check anything else
unless ($TransObj->Type eq "Comment") {
  return 0;
}
 
# Now get the custom field value for our transaction
my $TransCustomFieldName = "TransCustomFieldName";
my $TransCustomFieldValue = $transObj->FirstCustomFieldValue($TransCustomFieldName);
 
# Log it for debugging
# NOTE: I always log with the queue name because I may use the same type of routine in multiple queues
$RT::Logger->debug("$self->TicketObj->QueueObj->Name: $TransCustomFieldName = $TransCustomFieldValue");
 
# return 0 unless our value changed
return 0 unless ($TransCustomFieldValue != 0);
                                               
1;

Preparation Code

1;

Clean-up Code

my $TicketObj = $self->TicketObj;
my $TransObj = $self->TransactionObj;
my $QueueObj = $TicketObj->QueueObj;

# First get the current value of our Ticket Custom field we want to update
my $TransCustomFieldName = "TransCustomFIeldName";
my $TransCustomFIeldValue = $TransObj->FirstCustomFieldValue($TransCustomFieldName);
# Make sure to normalize your Transaction Custom Field Value if needed (in this case it isn't but if you are processing multiple Transaction Custom Fields it may be)
# if (!$TransCustomFieldValue) { $TransCustomFieldValue = 0; }

# This code looks different because it is pulled right from the Wiki with some simple modifications. Best to keep the same structure for maintainability.
my $CFName = "TicketCustomFIeldName";
my $CFValue = $TicketObj->FirstCustomFieldValue($CFName);
# Make sure you normalize your Ticket Custom Field Value too
if (!$CFValue) { $CFValue = 0; }
 
# Or whatever you want to do with it...
$CFValue += $TransCustomFieldValue;
 
my $DefaultValue = '0';
my $RecTransaction = 1;
$RT::Logger->debug("$QueueObj->Name: $CFName -> $CFValue");

my $CFObj = RT::CustomField->new($QueueObj->CurrentUser);
$CFObj->LoadByNameAndQueue(Name => $CFName, Queue => $QueueObj->id);
unless ($CFObj->id) {
    $CFObj->LoadByNameAndQueue(Name => $CFName, Queue => 0);
    unless ($CFObj->id) {
        $RT::Logger->debug("$QueueObj->Name: $CFName doesn't exist, Queue - " . $QueueObj->Name);
        return undef;
    }
}
my($result, $msg) = $TicketObj->AddCustomFieldValue(
                    Field => $CFObj->id,
                    Value => $CFValue,
                    RecordTransaction => $RecTransaction
	);
unless ($result) {
    $RT::Logger->debug("$QueueObj->Name: Couldn't set $CFValue as value for CF $CFName : $msg");
  return undef;
}
 
1;
 
NOTES:

1. Make sure you change the TransCustomFieldName variable to your actual field name
2. You may have to change the check to see if your field value changed. For my type of field in this scrip it was simple enough to do the test I'm doing. (Also, I'm a Perl newb so don't think this is pretty Perl.)
3. Make sure you change the CFName variable to your actual field name
4. Make sure you normalize your TicketCustomFieldValue variable just in case it hasn't been set yet. Additive values are never fun when you start with an unknown
5. MUST BE TransactionBatch
6. Use at your own peril, I haven't tested this code and it is freely given to the public domain as-is, enjoy!

I hoe that helps.

Thanks,

Mike

On Jun 4, 2012, at 10:42 AM, TheHoboKing wrote:

> 
> Hi Thomas,
> 
> Yes, I did attempt with TransactionBatch (and Create), the value obtained
> from the Transaction CF is always ' ' as if it's unable to capture the
> actual value.
> 
> Here's the code I was using, I've tried with different variation but was
> never able to get the value that was being added in the Trans-CFs.
> 
> As a test, I was simply trying to get the value from the Trans-CFs and right
> it back in a Ticket CF.
> 
> --------------
> Custom Condition:
> return 1;
> 
> Custom action preparation code:
> return 1;
> 
> Custom action cleanup code:
> my $ticket = $self->TicketObj;
> my $cf_obj = RT::CustomField->new( $RT::SystemUser );
> 
> my $cf_name = "ValueFrom_tr-cf";
> my $trcf_name = "tr-cf";
> 
> my $trcf_value = "1";
> 
> #Read and store the value of the Transaction CF "tr-cf"
> 
> $cf_obj->LoadByName(Name=>$trcf_name);
> $trcf_value = $ticket->FirstCustomFieldValue($trcf_name);
> 
> #Add the value to the Ticket CF
> $cf_obj->LoadByName(Name=>$cf_name);
> $ticket->AddCustomFieldValue(Field=>$cf_obj, Value=>$trcf_value,
> RecordTransaction=>0);
> 
> return 1;
> 
> ---------------------------------------------------
> 
> Any ideas on what could be going wrong?
> 
> Thank you
> 
> 
> Thomas Sibley wrote:
>> 
>> On 06/04/2012 09:46 AM, TheHoboKing wrote:
>>> After some research and trial and errors I finally realise extracting
>>> values
>>> from Transaction CFs is no longer possible.
>>> 
>>> The feature has been removed since RT 3.6 and doesn't seem to have been
>>> successfully implemented again. Mike seems to be the last reported
>>> successful extraction of CFs and such. (If anyone has a patch or
>>> successful
>>> scrip/solution to extract values from Trans-CFs, please by all means come
>>> forward and tell me how wrong I am)
>> 
>> You can absolutely read back out values from Transaction CFs.
>> Unfortunately we can't guess what you're doing or why it's not working
>> without seeing the code.  I suspect in your attempts you didn't follow
>> Mike's warning about needing to use the TransactionBatch stage for your
>> scrips.
>> 
>> Thomas
>> 
>> 
> -- 
> View this message in context: http://old.nabble.com/Append-Data-to-CustomFields-tp33920988p33958371.html
> Sent from the Request Tracker - User mailing list archive at Nabble.com.
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.bestpractical.com/pipermail/rt-users/attachments/20120604/a924a39b/attachment.htm>


More information about the rt-users mailing list