Avoid Duplicate Tickets By Email
In this example we want to configure a custom Email Rule, in order not to create a new operation, if one already exists in open status.
The “duplicate” email will be added as a comment to the already open ticket.
STEP1: SET THE MAIN FIELDS OF THE RULE
As a first step set the basic fields of the rule: Name, Priority, Mailbox, Status.
Set the ‘Apply On‘ field to ‘On Create‘, this will intercept all those emails that are not recognized as part of an already existing thread (for example through the pattern TICKET#).
Set, in the field ‘Related Model Alais‘ the model ‘DeepActivity – Activity‘, to sepcify that a comment will be added to an operation.
STEP2: CONFIGURE THE FILTER
Messages that meet all of the following conditions, when compared to an operation, will be considered duplicates:
- The sender is also the Requester User of the operation
- The subject of the email is the title of the ticket
- The operation is, at that moment, not closed/eliminated
To configure the filter, enter the following PHP code in the ‘Email Messages Filter‘ field
//JSON that manage what the querybuilder shows in this case 'Body Text equal READ CODE ABOVE'
/** START_RULES
{"condition":"AND","rules":[{"id":"body_text","field":"body_text","type":"string","input":"text",
"operator":"equal","value":"READ CODE ABOVE"}],"valid":true}
END_RULES */
//parse the sender and normalize the email address
$fromString = '';
$from = Deep::helper('deep_util')->parseEmailString($this->getMessage()->getFrom());
if (count($from)){
//get normalized address
$fromString = array_keys($from)[0];
//load the user corresponding to that email address
$fromUser = Deep::getModel('deep_admin/user')->loadByEmail($fromString);
//if exists retreive his username
$fromUsername = $fromUser && $fromUser->getId() ? $fromUser->getUsername() : $fromString;
//retreive the message subject
$mailSubject = $this->getMessage()->getSubject();
//retreive all open status
$openStatusValues = Deep::helper("deep_list")->getStatusValues(
Deep_Service_Model_Operation::LIST_CODE_STATUS,
Deep_List_Model_Listclass::TYPE_OPEN);
//retrieve a collection of operations that meet the following requirements
$operations = Deep::getResourceModel('deep_service/operation_collection')
->addFieldToFilter('status',['in' => $openStatusValues])
->addFieldToFilter('requester_username',['eq' => $fromUsername])
->addFieldToFilter('title',['eq' => $mailSubject]);
//if at least one exists
if(count($operations)){
//save its id on the request
Deep::register('custom_email_rule_operation_model_id',$operations->getFirstItem()->getId());
//save sender username on the request
Deep::register('custom_email_rule_message_from_username',$fromUsername);
//return true means that the message is a duplicate
return true;
//then the code in the field 'Set Main Model Values' is executed
}
}
//not all the conditions to define the message as duplicate have been met
return false;
STEP3: CONFIGURE/SET THE MAIN MODEL
Through the field ‘Set Main Model Values‘ set the operation, retrieved in the field ‘Email Messages Filter”, as main model to which the message must be added as comment.
Insert the following PHP code in the scripting area.
//JSON that manage what the querybuilder shows in this case 'Description equal READ CODE ABOVE'
/** START_RULES
{"condition":"AND","rules":[{"id":"description","field":"description","type":"string","input":"text",
"operator":"equal","value":"READ CODE ABOVE"}],"valid":true}
END_RULES */
//set message as non-creator. Because it doesn't create a new main model.
$this->getMessage()->setIsCreator(0);
//retreive operation id from the request (added in the code of the 'Email Messages Filter' field)
$operationId = Deep::registry('custom_email_rule_operation_model_id');
//load the operation and set it as main model
$this->setModel(Deep::getModel('deep_service/operation')->load($operationId));
return $this->getModel();
STEP4: CONFIGURE COMMENT CREATION
Through the field ‘Set Related Model Values‘ configure the creation of the comment in relation with the main model, set in the field ‘Set Main Model Values‘.
Inserting the following PHP code in the scripting area.
//JSON that manage what the querybuilder shows in this case 'Description equal READ CODE ABOVE'
/** START_RULES
{"condition":"AND","rules":[{"id":"description","field":"description","type":"string","input":"text",
"operator":"equal","value":"READ CODE ABOVE"}],"valid":true}
END_RULES */
//retreive operation id from the request (added in the code of the 'Email Messages Filter' field)
$operationId = Deep::registry('custom_email_rule_operation_model_id');
//delete operation id from the request
Deep::unregister('custom_email_rule_operation_model_id');
//retreive sender username from the request (added in the code of the 'Email Messages Filter' field)
$fromUsername = Deep::registry('custom_email_rule_message_from_username');
//delete sender username from the request
Deep::unregister('custom_email_rule_message_from_username');
//Create new Activity
$comment = Deep::getModel('deep_activity/activity');
//Set type Comment (1=>Comment, 2=>Worklog)
$comment->setType(1);
//Set the main model (operation in this example) alias
$comment->setModelAlias('deep_service/operation');
//set the id of the operation to which the comment should be added
$comment->setModelId($operationId);
//set the email body as Comment description
$comment->setDescription($this->getMessage()->getBody());
//set sender as user who created the comment
$comment->setCreatedBy($fromUsername);
//set comment as visible to end users
$comment->setPortalVisibility(1);
//save the comment
$comment->save();