AutomaticCustomFieldValue: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
m (2 revisions imported)
 
mNo edit summary
 
Line 3: Line 3:
Dan Dofton, Four County Library System, [mailto:ddofton@4cls.org ddofton@4cls.org]
Dan Dofton, Four County Library System, [mailto:ddofton@4cls.org ddofton@4cls.org]


PREP CODE
=== PREP CODE ===
 
<pre>
  my $requestor_address = $self-&gt;TicketObj-&gt;RequestorAddresses;
  my $requestor_address = $self-&gt;TicketObj-&gt;RequestorAddresses;
  my $domain = "example.org";
  my $domain = "example.org";
Line 13: Line 13:
     return 1;
     return 1;
  }
  }
   
  </pre>
 
CLEANUP CODE


=== CLEANUP CODE ===
<pre>
  my $T_Obj = $self-&gt;TicketObj;
  my $T_Obj = $self-&gt;TicketObj;
  my $requestor_address = $T_Obj-&gt;RequestorAddresses;
  my $requestor_address = $T_Obj-&gt;RequestorAddresses;
Line 35: Line 35:
   
   
  $CF_Obj-&gt;LoadByName( Name =&gt; $cf_name, Queue =&gt; '0',);
  $CF_Obj-&gt;LoadByName( Name =&gt; $cf_name, Queue =&gt; '0',);
    $RT::Logger-&gt;debug( "Loaded \$CF_Obj-&gt;Name = ". $CF_Obj-&gt;Name() ."\n" );
  $RT::Logger-&gt;debug( "Loaded \$CF_Obj-&gt;Name = ". $CF_Obj-&gt;Name() ."\n" );
  $CF_Obj-&gt;AddValueForObject( Object  =&gt; $T_Obj,
  $CF_Obj-&gt;AddValueForObject( Object  =&gt; $T_Obj, Content =&gt; $cf_value, );
                            Content =&gt; $cf_value, );
  return 1;
  return 1;
   
   
</pre>


== CustomField values based on incoming To: address ==
== CustomField values based on incoming To: address ==
Line 53: Line 53:
  return 1;
  return 1;
   
   
=== Custom Action cleanup code ===


Custom Action cleanup code:
<pre>
 
  my $to = $self-&gt;TicketObj-&gt;Transactions-&gt;First-&gt;Attachments-&gt;First-&gt;GetHeader("To");
  my $to = $self-&gt;TicketObj-&gt;Transactions-&gt;First-&gt;Attachments-&gt;First-&gt;GetHeader("To");
   
   
Line 76: Line 76:
  return 1;
  return 1;
   
   
</pre>


== CustomField values based on Requestor Address ==
== CustomField values based on Requestor Address ==
Line 88: Line 89:
   
   


Custom Action cleanup code:
=== Custom Action cleanup code ===


<nowiki>my $T_Obj = $self-&gt;TicketObj;
<pre>
my $T_Obj = $self-&gt;TicketObj;
  my $requestor_address = $T_Obj-&gt;RequestorAddresses;
  my $requestor_address = $T_Obj-&gt;RequestorAddresses;
  my $cf_value;
  my $cf_value;
Line 106: Line 108:
                             Content =&gt; $cf_value, );
                             Content =&gt; $cf_value, );
  return 1;
  return 1;
</nowiki>
</pre>


== Troubleshooting ==
== Troubleshooting ==
Line 114: Line 116:
  RT::Principal=HASH(0xa354040) HasRight called with no valid object (/usr/share/request-tracker3.4/lib/RT/Principal_Overlay.pm:293)
  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 =&gt; '0'.
This call failed due to Queue =&gt; '0'.



Latest revision as of 19:58, 13 August 2016

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