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. 

Banner SugarCRM Services link landing page


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

Copy to Clipboard

Below is code snippet to for logic hook class

Copy to Clipboard
  • $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

Copy to Clipboard

Create a class implementing the exact code logic for the requirement
./custom/modules/Accounts/update_industry.php

Copy to Clipboard

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

Copy to Clipboard

Create a class implementing the exact code logic for the requirement
./custom/modules/Cases/send_email.php

Copy to Clipboard

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

Copy to Clipboard
  • Create a class implementing the exact code logic for the requirement
    ./custom/modules/Orders/after_lineitem_add.php
Copy to Clipboard

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

Copy to Clipboard
  • Create a logic hook class in the below path
    ./custom/modules/Accounts/delete_contacts.php
Copy to Clipboard

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.

Contact us for a free assessment