Templates and custom ScripActions

Do I have to do something specific when creating a Scrip and using Action: User Defined in order to get RT to process the associated template with the scrip?

I have created a custom scrip, and just for testing purposes, I made the custom condition, custom action prep, and custom action cleanup all set to “return 1;”, and associated my desired template with the scrip. The scrip fires and returns successfully, but the template is never touched.

Are there specific steps I need to take in my custom action code in order to load, parse, and send notifications from a template?

Thanks,
Fran

Hi Fran,

Do I have to do something specific when creating a Scrip and using Action: User Defined in order to get RT to process the associated template with the scrip?

I have created a custom scrip, and just for testing purposes, I made the custom condition, custom action prep, and custom action cleanup all set to “return 1;”, and associated my desired template with the scrip. The scrip fires and returns successfully, but the template is never touched.

Are there specific steps I need to take in my custom action code in order to load, parse, and send notifications from a template?

Thanks,
Fran

I’m using v3.6.3, so this may not be accurate with your setup.

I know of ways to do what you want.

The way I prefer to do it is to embed the code into the template. I’ve
found this to be fairly easy to do and it involves less code. It can
also be trickier to debug because the scrip doesn’t tell you what action
is being taken - it’s all in the template. It does make the template
bigger. Almost all of my templates have embedded decision-making and
data-processing routines in them. Here’s a piece of one of my templates
that builds a custom acknowledgment e-mail.

===== BEGIN TEMPLATE CODE
{ ### Tells user that ticket has been resolved
my $FromAddress = ‘DNS Requests someaddress@domain’;
my $ContactAddress = ‘me@domain’;
my $OwnerName = $Ticket->OwnerObj->RealName;
my $have_rmks;
my $c_content;

Get last Correspond

my $Transactions = $Ticket->Transactions;
$Transactions->Limit( FIELD => ‘Type’, VALUE => ‘Correspond’ );
$Transactions->OrderByCols (
{ FIELD => ‘Created’, ORDER => ‘DESC’ },
{ FIELD => ‘id’, ORDER => ‘DESC’ },
);
my $CorrespondObj = $Transactions->First;
if ($CorrespondObj && $CorrespondObj->Id) {
$c_content = $CorrespondObj->Content;
chomp $c_content;
$have_rmks =
!$CorrespondObj->Attachments->First->GetHeader(‘Received’);
}

Lots of other code removed

my $AddressGroup = “From: $FromAddress”;
$AddressGroup .= “\nCc: $Cc” if $Cc;
$AddressGroup .= “\nBcc: $Bcc” if $Bcc;
$OUT = "$AddressGroup

The ticket that was opened for your request for host “$mName” has been
resolved by $OwnerName. If you have any questions about this, you can
contact us at $ContactAddress.
$remarks

Regards,
Your Friendly IT Staff";
}
===== END TEMPLATE CODE

Another way to do this that actually uses a user-defined action with a
template is to make the calls to the appropriate RT routines from within
your scrip code. I did this with one of my scrips.

I wanted to do some non-standard things with the recipients, so I
modified RT’s SetRecipients() routine and stuck it into my scrip, then I
call it and make calls to the Prepare() and Commit() routines to build
and send an e-mail using the designated template.

===== BEGIN SCRIP CODE

Valid e-mail for ticket, send acknowledgment

$self->SetRecipients();
$self->SUPER::Prepare();
$self->SUPER::Commit();

sub SetRecipients {

custom routine to do non-standard things with the recipients

}
1;
===== END SCRIP CODE

The above code snippet is at the end of my scrip’s “Custom action
preparation code” block.

Regards,
Gene

Gene,

I wouldn’t mind embedding the logic in the template, in fact I tried that at first, but my decision making requires that I have access to $self->TransactionObj->NewValue from my CustomField. I couldn’t seem to get at this from the template itself. I had trouble finding the right magic to access the Transaction’s NewValue.

I’ve seen code samples that use $Transaction within templates, but $Transaction->NewValue seems undef. Are you aware of how I could get at NewValue?
Or perhaps as an alternative, a way to populate a variable in the Custom Condition that would then be accessible from the Template? I’m sort of grasping at straws, but hoping that somehow I can get the Template to see the NewValue of my CustomField. Once I have that, I can retrieve the email listing of users from the appropriate RT group and the rest is easy, and I would not need any user-defined action at that point.

Thanks,
Fran

Fran, I just did a quick test with a simple template and a custom field.
If my understanding of what you want to do is correct, the stuff below
should work for you.

Gene,

I wouldn’t mind embedding the logic in the template, in fact I tried that at first, but my decision making requires that I have access to $self->TransactionObj->NewValue from my CustomField. I couldn’t seem to get at this from the template itself. I had trouble finding the right magic to access the Transaction’s NewValue.

I’ve seen code samples that use $Transaction within templates, but $Transaction->NewValue seems undef. Are you aware of how I could get at NewValue?
Or perhaps as an alternative, a way to populate a variable in the Custom Condition that would then be accessible from the Template? I’m sort of grasping at straws, but hoping that somehow I can get the Template to see the NewValue of my CustomField. Once I have that, I can retrieve the email listing of users from the appropriate RT group and the rest is easy, and I would not need any user-defined action at that point.

Thanks,
Fran

-----Original Message-----
From: rt-users-bounces@lists.bestpractical.com [mailto:rt-users-
bounces@lists.bestpractical.com] On Behalf Of Gene LeDuc
Sent: Friday, October 22, 2010 11:34 AM
To: rt-users@lists.bestpractical.com
Subject: Re: [rt-users] Templates and custom ScripActions

Hi Fran,

Do I have to do something specific when creating a Scrip and using
Action: User Defined in order to get RT to process the associated
template with the scrip?

Here is my scrip
Condition: User Defined
Action: Notify Requestors
Template: Tell Gene
Stage: TransactionCreate
Custom condition:
===== BEGIN SCRIP CODE

Trigger if custom value changed

return $self->TransactionObj->Type =~ /CustomField/i;
===== END SCRIP CODE

Here is my “Tell Gene” template
===== BEGIN TEMPLATE CODE
{ ### Testing custom field changes from within a template

Get old and new values from the Transaction

my $old = $Transaction->OldValue();
my $new = $Transaction->NewValue();
my $field = $Transaction->Field();

Get current value from the Ticket

my $curval = get_custom(“Comments”);

Build the e-mail

$OUT = "From: me <me@domain>To: me@domain
Subject: $New CF value

Using Transaction values
Field: '$field’
Old value: '$old’
New value: ‘$new’

Using Ticket values
Current value: ‘$curval’
";

Returns custom field value

get_custom($field_name)

sub get_custom {
my $target_name = $_[0];
my $val = $Ticket->FirstCustomFieldValue($target_name);
return $val if defined $val;
return undef;
}
}
===== END TEMPLATE CODE

Here is the email I got when I changed the value of my “Comments” CF
===== BEGIN EMAIL
From: me me@domain
To: me@domain
Subject: [myRT #4363] CF value

Using Transaction values
Field: '12’
Old value: '54321’
New value: ‘123’

Using Ticket values
Current value: ‘123’
===== END EMAIL

Thanks Gene, your code sample worked perfectly. It is much easier to use one of the built-in Notify actions and place the logic in the template, rather than try to write a scrip with all the logic to send notifications. Thanks for the guidance!

-Fran

Hello,

Template is a piece of text that can be used from your action code.
Look at SendMail.pm and ExtractCustomFieldValues extension. Both use
templates and in completly different way.

If you want to send some email from your action then probably the
right way is to record a comment or reply on the ticket from action
and let notification scrips do thier job.On Fri, Oct 22, 2010 at 7:15 PM, Francis L Fabrizio fabrizio@uab.edu wrote:

Do I have to do something specific when creating a Scrip and using Action: User Defined in order to get RT to process the associated template with the scrip?

I have created a custom scrip, and just for testing purposes, I made the custom condition, custom action prep, and custom action cleanup all set to “return 1;”, and associated my desired template with the scrip. The scrip fires and returns successfully, but the template is never touched.

Are there specific steps I need to take in my custom action code in order to load, parse, and send notifications from a template?

Thanks,
Fran

Best regards, Ruslan.