Every company’s aim is to use customer relationship management effectively and maximize productivity and enhance customer relationship. Personalizing CRM may need putting in business logic on certain events. Where as workflows can perfectly handle that, an alternate approach for more complicated situation is to develop logic hooks within the SugarCRM. These could handle situations like running complex computations, validation and informing business partners on events like creation and editing of a record. Let us look at logic hook in more details.
Logic Hook within SugarCRM
SugarCRM comes with a framework called Logic Hook framework which allows you to perform actions at the time of creating, editing and deleting of records. Logic Hooks can be implemented at different levels like application, module, user, Job Queue, API, etc.
In this article, we will see the implementation of Module Hooks in SugarCRM. Module hooks can be defined as an extension of or directly to the module.
- Module Extension Hooks: Module extension hooks are located in ./custom/Extension/modules/<module>/Ext/LogicHooks/. Hook actions defined will be executed for the specified events in the given module. Extensions are best used when creating customizations that may be installed in various environments. They help prevent conflicting edits that occur when modifying ./custom/modules/<module>/logic_hooks.php.
- Module Hooks: Module hooks are located in ./custom/modules/<module>/logic_hooks.php. Hook actions defined will be executed for the specified events in the given module. This path can be used for private development, however, it is not recommended for use with distributed modules and plugins.
Definition
All the logic hooks must have $hook_array and $hook_version variables defined. Below is the code snippet to define a module hook
<?php $hook_version = 1; //determines the version of logic hook framework $hook_array['<event_name>'][] = array( //string: The name of the event to append the action <process_index>, //Integer: The order of execution '<description>', //String: Short description of hook action '<file_path>', //String: path to logic hook file or null if using namespaces '<class_name>', //String: Name of the logic hook action class '<method_name>', //String: Name of the logic hook action method );
Below is code snippet to for logic hook class
<?php class className { function methodName($bean, $event, $arguments) { //logic } } ?>
- $bean: The object bean.
- $event: The current event.
- $arguments: Additional information related to the event
Let’s go through a few scenarios where a module hook events are used.
Before Save Logic Hook
Let’s consider a scenario where ABC organization needs, in ‘Accounts’ if the “account type” is “Customer” then the “Industry” value should be “HealthCare”. Since it is a data update in same module it is best achieved by before_save hook. “Before_save hook executes before a bean is saved into the db”.
Steps to declare ‘before_save’ hook
Declaring hook in hook_array. Create a php file with following code at the below path.
./custom/Extension/modules/Accounts/Ext/LogicHooks/update_industry.php
<?php $hook_array['before_save'][] = Array( 1, 'Delete related contact records', 'custom/modules/Accounts/update_industry.php', 'update_industry', 'update_industry' ); ?>
Create a class implementing the exact code logic for the requirement
./custom/modules/Accounts/update_industry.php
<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class update_industry { function update_industry(&$bean, $event, $arguments) { if($bean->account_type == "Customer"){ $bean->industry = "Healthcare"; } } } ?>
After Save Logic Hook
For our ABC organization if any new case is created, an email should be sent to the concerned assigned user. Since no changes need to be made into the bean, and only the data of the bean is to be used, its best to settle such task as ‘after_save’ hook. “After_save hook executes after a record is saved into the db”.
Steps to create after save hook
Declaring hook in hook_array. Create a php file with following code at the below path.
./custom/Extension/modules/Cases/Ext/LogicHooks/send_email.php
<?php $hook_array['after_save'][] = Array( 1, 'Send email to assigned person', 'custom/modules/Cases/send_email.php', 'send_email', 'send_email_to_assigned' ); ?>
Create a class implementing the exact code logic for the requirement
./custom/modules/Cases/send_email.php
<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class send_email { function send_email_to_assigned($bean, $event, $arguments) { if (!$arguments['isUpdate'] && $event == 'after_save') { $email = BeanFactory::newBean('Emails'); $email->name = $bean->name; $email->description_html = "Case {$bean->name} is assigned to you"; $user = BeanFactory::retrieveBean('Users', $bean->assigned_user_id); $email->to_addrs_arr[] = Array('email' => $user->emailAddress->getPrimaryAddress($user), 'display' => $user->name); $email->parent_type = 'Cases'; $email->parent_id = $bean->id; $email->save(); $email->send(); } } } ?>
In the above code, $arguments[‘isUpdate’] is used in the if condition. It holds a boolean value, the false value indicates the record is created and the true value indicates the record is updated. In our case, email should be sent only when the case is created. Inside the if condition an email is created with case details and $email->send() method is used to send mail to the assigned person.
After Relationship Add Logic Hook
Let’s consider the requirement, whenever a line item is added to order, ‘marked for review’ field in orders module should be checked. “After_relationship_add executes after a relationship is added between two records”.
Steps to create after relationship add hook
Declaring hook in hook_array. Create a php file with following code at the below path.
./custom/Extension/modules/Orders/Ext/LogicHooks/after_lineitem_add.php
<?php $hook_array['after_relationship_add'][] = Array( 1, 'Mark Order for review', 'custom/modules/Orders/after_lineitem_add.php', 'after_lineitem_add', 'mark_order_for_review' ); ?>
- Create a class implementing the exact code logic for the requirement
./custom/modules/Orders/after_lineitem_add.php
<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class after_lineitem_add { function mark_order_for_review($bean, $event, $arguments) { if($arguments['related_module'] == 'LineItems' && $bean->mark_for_review != true){ $bean->mark_for_review = true; $bean->save(); } } } ?>
In the above code $arguments contains additional details related to the event like module, related module, module id, related module id, link and relationship name. The logic needs to be executed only when line item is added. If condition satisfies only if related module is “LineItem” and “mark_for_review” field is not true. Inside the if condition the field is updated with true value and saved.
Before Delete Logic Hook
For this new scenario, the requirement is when an Account is deleted, all the contacts related to this accounts need to be deleted. In this ‘before_delete’ logic the code should fetch all the contact beans related to this account and mark the contacts as deleted.
Steps to create before delete hook
Logic needs to be triggered when an account is deleted. Create a logic hook in the below path.
./custom/Extension/modules/Accounts/Ext/LogicHooks/delete_contacts.php
<?php $hook_array['before_delete'][] = Array( 1, 'Delete related contact records', 'custom/modules/Accounts/delete_contacts.php', 'delete_contacts', 'delete_related_contacts' ); ?>
- Create a logic hook class in the below path
./custom/modules/Accounts/delete_contacts.php
<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class delete_contacts { function delete_related_contacts($bean, $event, $arguments) { if($bean->load_relationship('Contacts')){ $arr_conIds[] = $bean->Contacts->get(); foreach ($arr_conIds as $key => $id) { $contact_bean = BeanFactory::registerBean($id); $contact_bean->mark_deleted($contact_bean->id); $contact_bean->save(); } } } } ?>
Hooks allows us to list different hook actions to the required hook events allowing scope to automated features related to any particular module.
Evaluating SugarCRM for your business – We can help
Reach out to us so that we can assess and plan a road-map for your CRM implementation. Let’s build a system, which you will use for years to come.