OnCreateCheckCF: Difference between revisions

From Request Tracker Wiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Contributed by [http://wiki.bestpractical.com/view/Cris Cris]
Contributed by [[User:Cris70|Cris]]


=== Overview ===
=== Overview ===
Line 54: Line 54:


'''Content:'''
'''Content:'''
 
    Subject: Your ticket has been rejected due to a missing mandatory field.
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.
You sent a ticket with a missing piece of information. Since the missing information
    Please verify the informations you sent and then retry submitting the ticket.
is mandatory, the ticket has been rejected.
    If you feel your ticket has been rejected due to an error, please contact the administrator.
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.
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.
Line 83: Line 80:
In the "Custom condition" box enter this code:
In the "Custom condition" box enter this code:


<pre>
<syntaxhighlight lang="perl" line="1" >
    my $trans = $self-&gt;TransactionObj;
my $trans = $self->TransactionObj;
    return 0 unless $trans-&gt;Type eq "Create";
return 0 unless $trans->Type eq "Create";
 
my $ticket = $self->TicketObj;
 
my $Email_Rq = $ticket->RequestorAddresses;
      
      
    my $ticket = $self-&gt;TicketObj;
my $CustomFields = $ticket->QueueObj->TicketCustomFields();
   
while (my $CustomField = $CustomFields->Next()) {
    my $Email_Rq = $ticket-&gt;RequestorAddresses;
  my $nam = $CustomField->Name;
   
  my $typ = $CustomField->Type;
    my $CustomFields = $ticket-&gt;QueueObj-&gt;TicketCustomFields();
  my $vad = $CustomField->Pattern;
    while (my $CustomField = $CustomFields-&gt;Next()) {
 
      my $nam = $CustomField-&gt;Name;
  if ($vad == '(?#Mandatory)') {
      my $typ = $CustomField-&gt;Type;
    my $val = $ticket->FirstCustomFieldValue($nam);
      my $vad = $CustomField-&gt;Pattern;
    if ($val ne '') {
   
      next;
      if ($vad == '(?#Mandatory)') {
    } else {
          my $val = $ticket-&gt;FirstCustomFieldValue($nam);
      $ticket->SetStatus("rejected");
          if ($val ne '') {
              next;
          } else {
              $ticket-&gt;SetStatus("rejected");
          }
      }
     }
     }
  }
}
      
      
    return 1;
return 1;
    </pre>
</syntaxhighlight>


----
----

Latest revision as of 02:02, 7 July 2016

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!