AutomaticCustomFieldValue

From Request Tracker Wiki
Revision as of 16:03, 6 April 2016 by Admin (talk | contribs) (2 revisions imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This scrip will set a global custom field value according to a requestor's email address. We only want to set this field for users in a specific domain. You need to give requestors ModifyCustomFields rights. Took me too long to get this to work. Hope it helps someone.

Dan Dofton, Four County Library System, ddofton@4cls.org

PREP CODE

my $requestor_address = $self->TicketObj->RequestorAddresses;
my $domain = "example.org";
if ( $requestor_address !~ /$domain$/i ) {
    return 0;
}
else {
    return 1;
}

CLEANUP CODE

my $T_Obj = $self->TicketObj;
my $requestor_address = $T_Obj->RequestorAddresses;
my $cf_value;
my $CF_Obj = RT::CustomField->new($self->CurrentUser);
my $domain = "example.org";
my $cf_name = "customfieldname";

if ( $requestor_address =~ m#^(..)\.\w+\@$domain#i ) {
    # make cf_value = the first two characters of the email address if pattern is matched.
    $cf_value = uc $1;
    $RT::Logger->debug( $self . " cf_value = ". $cf_value ."\n");
}
else {
   $cf_value = "DEFAULT";
   $RT::Logger->debug( $self . " cf_value = ". $cf_value . "\n" );
}

$CF_Obj->LoadByName( Name => $cf_name, Queue => '0',);
    $RT::Logger->debug( "Loaded \$CF_Obj->Name = ". $CF_Obj->Name() ."\n" );
$CF_Obj->AddValueForObject( Object  => $T_Obj,
                            Content => $cf_value, );
return 1;

CustomField values based on incoming To: address

Because we have multiple email addresses feeding into one queue, I needed a way to set a custom field based on the To: address a ticket was sent to. This is what I came up with, based on Dan Dofton's code above and help from Robert Spier.

Travis Campbell

Condition: OnCreate Action: User Defined

Custom Action prep code:

return 1;

Custom Action cleanup code:

my $to = $self->TicketObj->Transactions->First->Attachments->First->GetHeader("To");

my $cf_value = "";
$cf_value = "Value1" if ($to =~ /^address1/i);
$cf_value = "Value2" if ($to =~ /^address2/i);
$cf_value = "Value3"  if ($to =~ /^address3/i);
$cf_value = "Value4"  if ($to =~ /^address4/i);
$cf_value = "Value5" if ($to =~ /^address5/i);

my $CF_Obj = RT::CustomField->new($RT::SystemUser);
my $cf_name = "Site";

$RT::Logger->debug( $self . " cf_value = ". $cf_value . "\n" );

$CF_Obj->LoadByName( Name => $cf_name, Queue => '0',);
   $RT::Logger->debug( "Loaded \$CF_Obj->Name = ". $CF_Obj->Name() ."\n" );
$CF_Obj->AddValueForObject( Object  => $self->TicketObj,
                           Content => $cf_value, );
return 1;

CustomField values based on Requestor Address

Expanding on the two examples above here's a scrip that extracts both the username and domain name portion from the Requestor's Email address, took an hour or so to knock up, hopefully it will save some time for non Perl heads like myself.

Condition: OnCreate Action: User Defined

Custom Action prep code:

return 1;

Custom Action cleanup code:

my $T_Obj = $self->TicketObj;
 my $requestor_address = $T_Obj->RequestorAddresses;
 my $cf_value;
 my $CF_Obj = RT::CustomField->new($self->CurrentUser);
 my $cf_name = "Custom Field Name"; #change this to the CF name
 
 
 $requestor_address =~ /(^.+)@([^\.].*\.[a-z]{2,}$)/;
 # $1 returns the username, $2 returns the domain name
 $cf_name = $2
 
 $CF_Obj->LoadByName( Name => $cf_name,);
    $RT::Logger->debug( "Loaded \$CF_Obj->Name = ". $CF_Obj->Name() ."\n" );
 $CF_Obj->AddValueForObject( Object  => $self->TicketObj,
                            Content => $cf_value, );
 return 1;
 

Troubleshooting

I found that this error started happening, possibly after an upgrade or some queue changes:

RT::Principal=HASH(0xa354040) HasRight called with no valid object (/usr/share/request-tracker3.4/lib/RT/Principal_Overlay.pm:293)

This call failed due to Queue => '0'.

$CF_Obj->LoadByName( Name => $cf_name, Queue => '0',);

Replaced with

$CF_Obj->LoadByName( Name => $cf_name );


RT 3.8 and up

Some working examples at SetCustomFieldViaMail