How to refer to a Custom field in a scrip's Custom condition?


#1

Hi

this is my first attempt at writing a custom condition for a scrip and I’m struggling to make it work. I started here https://rt-wiki.bestpractical.com/wiki/WriteCustomCondition and based on this:

# get ticket object 
my $ticket = $self->TicketObj;
# don't do anything if ticket is resolved 
return 0 if $ticket->Status eq 'resolved';
# otherwise run scrip action
return 1;

attempted this:

# get ticket object
my $ticket = $self->TicketObj;
# run scrip if Work Item is already set
return 1 if $ticket->CustomFieldValue(Field =>'Work Item Number') ne undef;
# get transaction object
my $transaction = $self->TransactionObj;
# run scrip if Work Item is being set
return 1 if $transaction->CustomFieldValue(Field =>'Work Item Number') ne undef;
# otherwise do nothing
return 0;

For the scrip I have the following set:
Condition: User defined (as above)
Action: Open Tickets
Template: Blank

I put the above together from a bunch of examples on the rt-wiki but I have never written even a lick of perl before so I feel like I am essentially flailing about here.

I have a custom field “Work Item Number” that is always empty when a ticket is created. When a work item number is allocated to the ticket we want that ticket to change from new to open, and not before. Tickets with a value in the “Work item number” custom field should be set to ‘open’ so I want the scrip to fire when either the ticket already has a value or a transaction sets a value.

It doesn’t matter what value I set to the CF the status of the ticket never changes.

I tried changing the action to the following custom action:

# get ticket object
my $ticket = $self->TicketObj;
# set ticket to open
TicketObj->SetStatus( 'open' );
return 1;

but still no joy.

Can anyone suggest where I am going wrong? Is the logic broken? Or perhaps some suggested reading on how to learn to code this?


#2

I believe the API call you’re looking for is FirstCustomFieldValue('Field Name');, so $ticket->FirstCustomFieldValue('Work Item Number');


#3

Thanks Craig,

I have been away from this, my apologies for not responding sooner. How can I log/debug this? I don’t see any change in the ticket status. Should I be using the pre-defined Action ‘Open Tickets’ or a custom action commit code? I tried both.

Currently I have this:

Custom condition.

# get ticket object
my $ticket = $self->TicketObj;
# run scrip if Work Item is already set
return 1 if $ticket->FirstCustomFieldValue('Work Item Number') ne undef;
# get transaction object
my $transaction = $self->TransactionObj;
# run scrip if Work Item is being set
return 1 if $transaction->FirstCustomFieldValue('Work Item Number') ne undef;
# otherwise do nothing
return 0;

Custom action commit code:

# get ticket object
my $ticket = $self->TicketObj;
# set ticket to open
TicketObj->SetStatus( 'open' );
return 1;

How can I determine what these are returning? I am happy to do the reading if you can suggest where, rather than asking here to do the work for me…


#4

TicketObj is not defined, I believe you want $ticket you can log output using RT::Logger->error('String or scalar') and then check your logs.