Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Last active June 2, 2016 13:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimonetti/36b09205490284a91646 to your computer and use it in GitHub Desktop.
Save esimonetti/36b09205490284a91646 to your computer and use it in GitHub Desktop.
SugarCRM - Inbound Email Case Creation - Custom Record Assignment and Case reopening - custom/modules/Emails/inboundEmailsLogic.php
<?php
// Enrico Simonetti
// 11/09/2014
// Tested on SugarCRM 6.5.17
// Create standard Inbound Email to Case functionality, using by default an empty team ($inbound_cases_team) for round robin assignment
// Then define the custom assignment rules
/*
Logic hook entries will look something like this... if entered manually
$hook_array['before_save'][] = Array(2, 'reopenCase', 'custom/modules/Emails/inboundEmailsLogic.php','inboundEmailsLogic', 'reopenCase');
$hook_array['before_save'][] = Array(3, 'customCaseAssignment', 'custom/modules/Emails/inboundEmailsLogic.php','inboundEmailsLogic', 'customCaseAssignment');
*/
class inboundEmailsLogic
{
public $inbound_cases_team = 'Inbound Cases';
public $assignment_cases_team = 'Global';
// before save on emails
public function reopenCase(&$bean, $event, $args)
{
if(!empty($bean->parent_type) && !empty($bean->parent_id))
{
// if the email is related to a case
if($bean->parent_type == 'Cases')
{
$case = BeanFactory::getBean('Cases', $bean->parent_id);
// if the status is not assigned and the email is unread
if($case->status != 'Assigned' && $bean->status == 'unread')
{
$GLOBALS['log']->debug('inboundEmailsLogic->reopenCase - reopening case '.$case->id);
// set case status to assigned, maybe notify user with a workflow?
$case->status = 'Assigned';
$case->save();
}
}
}
}
// before save on emails
public function customCaseAssignment(&$bean, $event, $args)
{
// if the email is related to a case, it is inbound and unread
if (!empty($bean->parent_id) && !empty($bean->fetched_row) && $bean->parent_type == 'Cases' && $bean->type == 'inbound' && $bean->stat
us == 'unread'
&& $bean->fetched_row['parent_type'] != 'Cases' && $bean->fetched_row['parent_id'] != $bean->parent_id)
{
$case = BeanFactory::getBean('Cases', $bean->parent_id);
if(!empty($case->id))
{
// find if our default inbound empty team exists...
$inbound_team = BeanFactory::getBean('Teams');
$inbound_team->retrieve_by_string_fields(array('name' => $this->inbound_cases_team));
$assignment_team = BeanFactory::getBean('Teams');
$assignment_team->retrieve_by_string_fields(array('name' => $this->assignment_cases_team));
if(!empty($inbound_team->id) && !empty($case->team_id) && $inbound_team->id == $case->team_id && !empty($assignment_team->id))
{
$GLOBALS['log']->debug('inboundEmailsLogic->customCaseAssignment - routing case '.$case->id);
// assign both the email and the case to the correct team
$bean->team_id = $bean->team_set_id = $assignment_team->id;
$case->team_id = $case->team_set_id = $assignment_team->id;
// now complete the custom case routing...
if($case->assigned_user_id == '')
{
// custom logic goes here... now let's assign the email and the case to the Admin as an example
$case->assigned_user_id = 1;
$bean->assigned_user_id = 1;
}
$case->save();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment