Condition “OnCorrespond”

Do you want your FireFlow workflow to handle incoming emails? Because it is very useful to be able to change the status of tickets, or update fields in tickets, on the basis of the contents of an incoming email. A simple example is approving or rejecting a ticket via email. The condition you need to use for your scrip is “OnCorrespond” another name might as well be “OnIncomingEmailForThisTicket”. Once you set your custom scrip to use the condition “OnCorrespond”, you can use code to access the email content. The example lines of code below show how this is done:

 

# get the 'from' email address

my $from = $self→TransactionObj→Attachments→First→GetHeader('From');

# get the 'subject' text from the email

my $subject = $self→TransactionObj→Subject;

# get the message body content (including attachments if any)

my $body = $self→TransactionObj→Content;

 

NOTE: FireFlow processes the incoming emails before they get to your scrip. And the purpose of that pre-processing is to determine which ticket the email was intended for. The email subject needs to contain the FireFlow ticket ID. The default form of the ticket ID in the subject is: [FireFlow #123]. Emails that contain that ticket ID format will be recognised by FireFlow and will be passed to your custom OnCorrespond scrip with the ticket object bound to the scrip.

 

The scrip below is a comprehensive example of the use of the OnCorrespond condition.

 

use strict;

use warnings;

 

$RT::Logger->info("start OnCorrespond scrip");

 

my $ticket = $self->TicketObj;

my $transaction = $self->TransactionObj;

## retrieve original message

my $to = $transaction->Attachments->First->GetHeader('To');

my $from = $transaction->Attachments->First->GetHeader('From');

my $subject = $transaction->Subject;

my $body = $transaction->Content;

 

$RT::Logger->debug("Received an email from: $from that is to: $to with subject: $subject");

 

if($subject =~ /APPROVED/ || $subject =~ /REJECT/ ){

  my $action="Rejected";

  if($subject =~ /APPROVED/){

    $action="Approved";

  }

  $RT::Logger->debug("Action found in the email is:[$action].");

  # check and see if this is an email from the ticket requestor's manager

  my $manager = $self->TicketObj->FirstCustomFieldValue('Manager Email');

  $RT::Logger->debug("Checking for match of email from: $from and requestor manager is:[$manager]");

  if(defined $manager && defined $from && $from =~ /$manager/){

    # if in status pre approve then record the approval and then move to plan status

    if($ticket->Status eq 'pre approve'){

      my( $result, $msg ) = $self->TicketObj->AddCustomFieldValue( Field => 'Manager Approved', Value => $action, RecordTransaction => 1 );

      unless( $result ) {

        $RT::Logger->error("Could not set CF:[Manager Approved] to:[$action]: ".$msg );

      }else{

        $RT::Logger->debug("The requestor manager $action the ticket. Now moving ticket status.");

        my $status='rejected';

        if($action eq 'Approved'){

          $status = 'open';

        }

        my ($result, $msg) = $ticket->SetStatus( Status => $status , Force => 1);

        $RT::Logger->debug("Result: [$result] Msg:[$msg]");

        unless( $result ) {

          $RT::Logger->error("Could not change ticket status. Reason: ".$msg );

        }elsif($action eq 'Approved'){

          # set request template ID and name as desired

          my $templateID = '609';

          $RT::Logger->debug("Setting Ticket Template ID CF to: [$templateID].");

          my( $result, $msg ) = $self->TicketObj->AddCustomFieldValue( Field => 'Ticket Template ID', Value => $templateID, RecordTransaction => 1 );

          unless( $result ) {

            $RT::Logger->error("Could not set Ticket Template ID CF to: [$templateID]. Reason: ".$msg );

          }else{

            my $templateName = 'Multi Approval Request';

            $RT::Logger->debug("Setting Ticket Template Name CF to: [$templateName].");

            my( $result, $msg ) = $self->TicketObj->AddCustomFieldValue( Field => 'Ticket Template Name', Value => $templateName, RecordTransaction => 1 );

            unless( $result ) {

              $RT::Logger->error("Could not set Ticket Template Name CF to: [$templateName]. Reason: ".$msg );

            }

          }

        }

      }

    }

  }

}

$RT::Logger->debug("finished scrip");

return 1;

 

Note that scrip changes the state of the ticket to plan or rejected if those keywords appear in the subject of the received email. The scrip also changes the request template, which may not suit your particular purposes.

For additional assistance feel free to contact Wantegrity at inquiry@wantegrity.com