[rt-users] Custom script to set custom field value

Craig Ringer craig at 2ndquadrant.com
Mon Jul 22 22:26:10 EDT 2013


On 07/20/2013 06:58 AM, matt at es-msat.org wrote:
> Hello!
> 
> I need to create two scripts that will set a custom field value for
> tickets that contain New Hire: or Termination: in the subject line. I
> only need it for one queue and I want it to trigger when a ticket is
> created or moved into that specific queue. I recently took over
> management of an RT instance from someone who left the company. I have
> never created customs scripts for RT. I have no idea if this is possible
> and I have no idea where to start. Any assistance would be greatly
> appreciated!

Writing scrips is pretty simple for things like this. You can start with
a scrip that's defined in the database - go to the queue page, then the
scrips tab. Create a new scrip with Scrips->Create.  Set the condition
to "On create" (since that seems to be when you want it to run) and the
action to "User defined". The template can be "blank" since you don't
need to generate template output for this action.

You'll now have Prepare and Commit sections to the action, in which you
can write Perl code to decide whether the action should run (Prepare)
and perform the action (commit). In this case the Prepare section would
test the ticket subject and the commit field would set the CF value.

There's some useful info on writing custom actions here:

http://requesttracker.wikia.com/wiki/WriteCustomAction

A reasonable Prepare test might be (untested):

    $self->TicketObj->Subject =~ '(New Hire|Termination):'


and to set the CF in Commit, something like:

    $self->TicketObj->AddCustomFieldValue(
        Field => 'MyCF',
        Value => 'Whatever'
    );

If you want to set the CF based on the data extracted from the subject
that's Perl Regexp 101; see 'perldoc perlre'.



BTW, I recommend that you learn how to package them into proper RT
extensions to save yourself pain and hassle down the track. It takes
some learning if you've never written Perl modules before, but it's
worthwhile so you can easily manage your modules in git or whatever your
preferred SCM is. It's saved me a lot of hassle, especially since I
maintain a test RT instance for developing extensions on. I posted some
notes on creating RT extensions here recently that I really need to turn
into a doc on wikia.

-- 
 Craig Ringer                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services



More information about the rt-users mailing list