Sugar Community Edition 5.2 Documentation
Sugar Developer Guide
Chapter 4 Customizing Sugar : Business Logic Hooks
Custom Logic (or "Business Logic Hooks") allows you to add functionality to certain actions, such as before a bean save, in an upgrade-safe manner. This is accomplished by defining hooks, which are placed in the custom/ directory, which will be called in the SugarBean in response to events at runtime. Because the code is located separate from the core SugarCRM code, it is upgrade-safe.See this SugarForge project for a complete example of a useful business hook.The code that declares your custom logic is located in: custom/modules/<CURRENT_MODULE>/logic_hooks.php. The format of the declaration follows.All logic hooks should define the $hook_version that should be used. Currently, the only supported $hook_version is 1.
o name: the name of the event that you are hooking your custom logic to
o array: an array containing the parameters needed to fire the hookThe hooks are processed in the order in which they are added to the array. The first dimension is simply the current action, for example before_save. The following hooks are available:
o Fired before a record is saved.
Note: With certain modules, like Cases and Bugs, the human-readable ID of the record (like the case_number field in the Case module), is not available within a before_save call. This is because this business logic simply hasn't been executed yet.
o Fired after a record is saved.
Note: With certain modules, like Cases and Bugs, the human-readable ID of the record (like the case_number field in the Case module), is not available within a before_save call. This is because this business logic simply hasn't been executed yet.
o $bean - $this bean passed in by reference.
o $event - The string for the current event (i.e. before_save)
o $arguments - An array of arguments that are specific to the event.There are no directives in the manifest.php file to safely install business logic hooks. You have to do it via the pre_install or post_install scripts. To do this, create a top-level directory in your installer called 'scripts' and in that directory place a file called pre_install.php.The check_logic_hook_file() command is looking for three things. First is the module that you want this hook to affect, in this case "Accounts". The second is the hook itself, in this case "after_retrieve". The third part is an array which mirrors the $hook_array like this:Of course you can call the check_logic_hook_file() command as many times as you want. The logic behind the command check to make sure that your hook isn't already set and if it is then it replaces it, making it pretty upgrade-safe. If you have any questions about installing logic hooks look for me in the forums.Above we showed how to create a custom_logic_hook that runs the function updateDescription() from the class updateDescription (those don't have to be the same, they just are in this example) in the PHP file updateDescription.php. Below is the actual script from that PHP file.You see that the $bean is fed into the function and allows access to all of the fields for the currently selected record. Any changes you make to the array will be reflected in the actual data. For example in this script we are changing the description field. As you see above there is
o In order to compare new values with previous values to see whether a change has happened, use $bean->fetched_row['<field>'] (old value) and $bean-><field> (new value) in the before_save logic hook.
o Make sure that the permissions on your logic_hooks.php file and the class file that it references are readable by the Web server. If this isn’t done, the files will not be read by the SugarCRM application and your business logic extensions will not work.
For example, for *nix developers who are extending the Tasks logic, that means you should do the following command for the logic_hooks file and the same command for the class file that will be called.
o Make sure that the entire custom/ directory is writable by the Web server, otherwise the logic hooks code will not work properly.
Copyright 2004-2009 SugarCRM Inc.
Product License
User Contributed Comments
No user comments found.Add a Comment




