Thursday, July 28, 2011

SugarCRM - Adding a Logic Hook for the Accounts Module

I am keeping track of some information for accounts in a separate database outside of the CRM.  When account information is changed, such as who it is assigned to, I need to be notified so I can capture those changes and make them to the other database.  In order to do that, I had to add a logic hook.  This entry will explain how I did that.

The first thing I did was create a file that defines an entry point into the hook for the Accounts module.  I did this in custom/modules/Accounts/ in a file called logic_hooks.php.  The code I used to define the hook is the following:

<?PHP
$hook_version =1;
$hook_array = Array();

$hook_array['after_save'] = Array();
$hook_array['after_save'][] = Array(1, 'TestHook', 'custom/modules/Accounts/AccountsLogicHook.php', 'AccountsLogicHook', 'onTestHookAfterSave');
?>

The last line defines the hook.  Notice the parameter 1.  This defines an order in case you have multiple hooks defined.  The text TestHook is a label that you can define as some string to identify the hook.  The next parameter custom/modules/Accounts/AccountsLogicHook.php lists the file that the class is located where your method is in.  In other words, it is the file where the code that you want to execute is located.  The next parameter AccountsLogicHook defines the class that the method is in.  The last parameter onTestHookAfterSave is the name of the method.  That is all I need in this file.

In the last line I also define the action for which I would like the hook to fire.  The action I have defined is after_save.  There is a list of available hooks in the documentation, but here is a sample of them:

after_ui_frame
after_ui_footer
after_round_trip
before_delete
after_delete
before_save
after_save

Now, the second file I created was custom/modules/Accounts and it is also in custom/modules/Accounts.  Notice how this matches up with the third parameter up above.  In the file, i put the following code:

<?PHP
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

class AccountsLogicHook
{
        function onTestHookAfterSave(&$bean, $event, $arguments)
        {
                $GLOBALS['log']->fatal("The account hook worked!");
        }
}

?>

Notice how I named a class as specified as the 4th parameter of my array above.  I also named my method as specified as the 5th parameter of my array above.  This method will put an entry in the log file when fired.  


1 comment:

  1. For anyone else that comes across this great post trying to get a logic hook work here is the list of all of the logic hooks: http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_6.5/04_Customizing_Sugar#Business_Logic_Hooks

    Unfortunately, there are some hooks missing in that list such as after_relationship_add and after_relationship_delete. Easiest way to find the true list of all hooks is to search the code base for "call_custom_logic" in all *.php files.

    ReplyDelete