SugarCRM Account Re-assign Logic Hook

This SugarCRM Logic Hook was used to overcome a common issue with SugarCRM. SugarCRM CE does not automatically reassign an Account’s related contacts or opportunities when the Account is reassigned. This often creates visibility/usability issues due to the lack of permissions (roles). The logic hook below reassigns the Account’s related contacts and opportunities saving you the time of having doing it manually as well as preventing multiple contact issues. This logic hook also manages a reassigning mapping to general store users based on status set.

<?php
// custom/modules/Accounts/logic_hooks.php
$hook_version = 1;
$hook_array = Array();
$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'updateAssignedUser', 'custom/modules/Accounts/AccountLogicHook.php', 'AccountLogicHook', 'updateAssignedUser');
$hook_array['after_save'][] = Array(2, 'updateAssignedUserRelated', 'custom/modules/Accounts/AccountLogicHook.php', 'AccountLogicHook', 'updateAssignedUserRelated');
?>
<?php
// custom/modules/Accounts/AccountLogicHook.php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class AccountLogicHook {

    function updateAssignedUser(&$bean, $event, $arguments) {

        // Update the assigned user based on the account's custom status field.
        $status_to_user_map = array(
            'Recycle' => 'marketing',
            'Recycled' => 'marketing',
            'Dead' => 'deadtargets',
        );
        // Find the Account's custom status field value.
        if (!empty($bean->fetched_row['account_status_c'])) {
            $status = $bean->fetched_row['account_status_c'];
        } elseif (!empty($bean->fetched_row['status_c'])) {
            $status = $bean->fetched_row['status_c'];
        } elseif (!empty($bean->status)) {
            $status = $bean->status;
        } else {
            $status = '';
        }

        if (isset($status_to_user_map[$status])) {

            require_once('modules/Users/User.php');
            $user = new User();
            $user_id = $user->retrieve_user_id($status_to_user_map[$status]);

            if(!empty($user_id)) {

                $bean->assigned_user_id = $user_id;
                $bean->save(FALSE);

                if (empty($_REQUEST['massupdate']) && $_REQUEST['module'] == 'Accounts' && $_REQUEST['action'] == 'Save' &&;
                    $_REQUEST['return_module'] == 'Accounts' && $_REQUEST['return_action'] == 'DetailView') {
                        SugarApplication::redirect('index.php?module=Accounts&action=index'); // contains header and exit
                }

            } // if $user_id

        } // if isset

    } // end of updateAssignedUser

    function updateAssignedUserRelated(&$bean, $event, $arguments) {

        // Update Related Contacts and Opportunities that have a 1-to-many relationship from the Account
        $user_id = $bean->assigned_user_id;

        if(!empty($user_id)) {

            // Find the Related Contacts
            require_once('modules/Contacts/Contact.php');
            $contacts = $bean->get_linked_beans('contacts', 'Contact');
            foreach ($contacts as $contact) {
                $contact->assigned_user_id = $user_id;
                $contact->save(FALSE);
            }

            // Find the Related Opportunities
            require_once('modules/Opportunities/Opportunity.php');
            $opportunities = $bean->get_linked_beans('opportunities', 'Opportunity');
            foreach ($opportunities as $opportunity) {
                $opportunity->assigned_user_id = $user_id;
                $opportunity->save(FALSE);
            }

        }

    } // end of updateAssignUserRelated

}
?>
Updated 01/10/2010 to use the SugarCRM bean save() method.

You must be logged in to post a comment.