OnCreateCheckCF

From Request Tracker Wiki
Jump to navigation Jump to search

Contributed by Cris

Overview


You may want to create a mandatory custom field, so that people will have to fill the value before being able to submit a new ticket. However, if you define a cf as mandatory, only people creating tickets from the web interface will be forced to fill the value. Those creating a new ticket using the mail gateway won't notice the presence of the mandatory cf, and the ticket will be created anyway.

My solution consists of three pieces:

  • the ExtractCustomFieldValues extension, needed to be able assign a value to the cf through an email message
  • an OnCreate scrip which checks if the cf is present, and eventually marks the ticket as rejected and notifies the requestor
  • a custom template to use when notifying the requestor about a rejected ticket

This is implemented on RT 3.8.4.

How to setup OnCreateCheckCF in RT3


1. Custom field creation

First create a new custom field: Configuration -> Custom Fields -> Create

Name: cfTest (you can use the name you want)

Description: test mandatory custom field

Type: Fill in one text area (you can use the type you want)

Validation: (?#Mandatory).

Now "Save Changes".

On the "Applies To" page select the queues where you want to enable the custom field, then on the "Group Rights" give the "ModifyCustomField" right to the "Everyone" system group (if you want everyone to be able to create tickets on those queues).

Note that this is just a sample mandatory custom field. The scrip below is generic and will check for the presence of any and all mandatory ticket custom fields.


2. Configure the ExtractCustomFieldValues

Follow the instructions in the ExtractCustomFieldValues section


3. Creating the template

Go to Configuration -> Global -> Templates -> New

Name: Error: no mandatory CF

Description: Inform the user that the ticket has been rejected due to a missing mandatory custom field

Content:

    Subject: Your ticket has been rejected due to a missing mandatory field.
    You sent a ticket with a missing piece of information. Since the missing information
    is mandatory, the ticket has been rejected.
    Please verify the informations you sent and then retry submitting the ticket.
    If you feel your ticket has been rejected due to an error, please contact the administrator.

Obviously this is only an example, you can write what you want. Just be sure to have the "Subject:" line as the very first line, and to leave a blank line after that.


4. Creating the Scrip

Go to Configuration -> Queues (or Global if you want this for all queues) -> (QueueName) -> Scrips -> New scrip

Description: OnCreateCheckCF

Condition: User Defined

Action: Autoreply To Requestors

Template: Global template: Error: no mandatory CF

Stage: TransactionBatch

In the "Custom condition" box enter this code:

my $trans = $self->TransactionObj;
return 0 unless $trans->Type eq "Create";

my $ticket = $self->TicketObj;

my $Email_Rq = $ticket->RequestorAddresses;
    
my $CustomFields = $ticket->QueueObj->TicketCustomFields();
while (my $CustomField = $CustomFields->Next()) {
  my $nam = $CustomField->Name;
  my $typ = $CustomField->Type;
  my $vad = $CustomField->Pattern;

  if ($vad == '(?#Mandatory)') {
    my $val = $ticket->FirstCustomFieldValue($nam);
    if ($val ne '') {
      next;
    } else {
      $ticket->SetStatus("rejected");
    }
  }
}
    
return 1;

That's all folks!


P.S.: Thanks to the user who spotted a typo in my scrip!