Avoid Duplicate Service Operations by Email
This guide walks you through configuring a custom email rule that prevents a new Service Operation from being created when one already exists in open status. Instead, the duplicate email is added as a comment to the existing open Service Operation.
Step 1: Set the Main Fields of the Rule
Set the basic fields of the rule: Name, Priority, Mailbox, and Status.
Set the Apply On field to On Create to intercept all emails that are not recognized as part of an existing thread (for example, through the pattern TICKET#).
Set the Related Model Alias field to DeepActivity - Activity, to specify that a comment will be added to a Service Operation.

Step 2: Configure the Filter
Messages that meet all of the following conditions are considered duplicates:
- The sender is the Requester User of the Service Operation
- The subject of the email matches the title of the Service Operation
- The Service Operation is not currently closed or eliminated
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 retrieve his username
$fromUsername = $fromUser && $fromUser->getId() ? $fromUser->getUsername() : $fromString;
//retrieve the message subject
$mailSubject = $this->getMessage()->getSubject();
//retrieve 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;
Step 3: Configure the Main Model
In the Set Main Model Values field, set the Service Operation retrieved in the filter as the main model to which the comment will be added.
//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);
//retrieve 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();
Step 4: Configure Comment Creation
In the Set Related Model Values field, configure the creation of the comment in relation to the main model.
//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 */
//retrieve 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');
//retrieve 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();