Thursday 15 January 2009

Request Tracker scrip that involves the Category of a Custom Field

Our Request Tracker works for users in many countries, which are all assigned to areas. There is an IT administrator for each area, which should be added to the CC list for the request that is from one of the countries in his/her area.

We have created a custom field, and used the category to group the countries by areas.

We needed a scrip with an action that needs to know the category of the currently selected value of a custom field.


Condition: User Defined
Action: User Defined
Template: Global Template: Blank
Stage: TransactionCreate


Custom condition:
my $CFName = 'Field/Country';

my $CF = RT::CustomField->new( $RT::SystemUser );
$CF->LoadByNameAndQueue( Name => $CFName, Queue => $self->TicketObj->Queue );

if ( (($self->TransactionObj->Type eq "CustomField" && $self->TransactionObj->Field == $CF->id)
||
$self->TransactionObj->Type eq "Create" ) &&
($self->TicketObj->FirstCustomFieldValue('Field/Country')) )
{
return 1;
}
return 0;


Custom action preparation code:
my $CFName = 'Field/Country';
# for FirstCustomFieldValue see in Record.pm
my $fieldcountryVal = $self->TicketObj->FirstCustomFieldValue($CFName);
my $aita_email = '';
my $area = '';

#see CustomFieldValue_Overlay.pm, sub Category

my $CFObj = new RT::CustomField($RT::SystemUser);
$CFObj->Load($CFName);
my $valuesObj = $CFObj->ValuesObj();
while (my $myValue = $valuesObj->Next) {
if ($myValue->Name eq $fieldcountryVal)
{
#$RT::Logger->info($fieldcountryVal." belongs to area ".$myValue->Category);
$area = $myValue->Category;
}
}

if ($area eq "Some Area")
{
$aita_email = 'admin@somearea.ourdomain.org';
}
elsif ($area eq "Another Area")
{
$aita_email = 'admin@anotherarea.ourdomain.org';
}
#if ($aita_email ne '')
{
my ($ret, $msg) = $self->TicketObj->AddWatcher( Type => "Cc", Email => $aita_email);

if ($ret) {
#$RT::Logger->info("scrip: New watcher added to ticket");

} else {
#$RT::Logger->error("scrip: Failed to add new watcher to ticket");
}
}

return 1;

Custom action cleanup code:
return 1;