Logic Hooks

Logic Hooks in SuiteCRM: How to Create it?

By Mayank Puri
5 minutes

The Most significant thing to do is making a CRM software Personalized for certain events. Yes, Workflow is a decent option for handling and managing things effectively. But if a Developer gets into a complicated requirement then nothing can help them except “Logic Hooks”. Every event will be updated to your records like if you create and edit any Business record. 

What is SuiteCRM Logic Hooks?

In very simple words, Logic Hooks allow a user to add actions to any event like a user can use this development strategy for creating, editing, and deleting any business record. It helps in executing the whole process when any event is triggered. An Event can be summarised as Save, delete, restore, retrieve, etc. 

Are you curious to know the Capabilities of this Development strategy? 

  1. It helps in Modifying each Business record before/after it gets saved on Database. 
  2. Logic Hooks can update other Database/third party sources based on actions performed in your CRM. 
  3. Sending of Email messages to any record. 

How can you Create Logic Hooks in SuiteCRM?

Add the below code:

$hook_array[‘before_save’][]=Array(1,’assignUser’,’custom/modules/Cases/Example.php’, ‘UpdateLogichookClass’, ‘updateLogichooksmethod’);

Let us create a new file on Path custom/modules/Cases/Example.php , open this file and put below content into that.

 <?php

Class UpdateLogichookClass  {
      function updateLogichooksmethod( $bean, $event, $arguments) 
      {
           //Query ACCOUNTS table for assigned_user_id value of parent Account
           $case_id = $bean->id;
           $acct_id = $bean->account_id;
          $query =  "SELECT accts.assigned_user_id FROM accounts accts ";
          $query .= "WHERE accts.id = '$acct_id' LIMIT 1";
          $results = $bean->db->query($query, true);
          $row = $bean->db->fetchByAssoc($results);
          $user_id = $row['assigned_user_id'];

           //Change assigned_user_id value on Case
           $bean->assigned_user_id = $user_id;
     }
}
?>

All Three Contexts of Logic Hooks

The top Three context types you need to know: Applications Hooks, User Hooks, and Module Hooks. 

User Level Hooks

This context executes the logic for user action for Log-in and Log-out time. 

Types of User Level Hooks are:

  • after_login
  • after_logout
  • before_logout 
  • login_failed
  • We have a Great example of SuiteCRM User Activity Tracker Product here. If any user enters into the CRM software the hook will add action to the event. The whole team will get to know the log-in time of the user and Log-out time too. Even with the accurate time too.

Module Level Hooks

This context executes the logic for a specific Module of SuiteCRM software. It is located in

./custom/Extension/modules/<module>/Ext/LogicHooks/my_hook_1.php

Users are free to take any name of a file (my_hook_1.php) as this is part of the extension.

Types of Module Hooks are

  • after_delete

Here we would like to share an Example of SuiteCRM Recycle Bin Product. A user deletes a Business record from a module of SuiteCRM. The After_delete logic hook easily gets executed when a record gets deleted. Hook will add the user action on that event.

  • before_delete

Let’s understand this with an example of our unrivaled Plugin Google Calendar Integration with SuiteCRM. A user deleted a few client meeting details on SuiteCRM. So, before the delete process gets updated on the database of SuiteCRM, the action will execute first on Google Calendar platform. 

  • after_relationship_delete
  • after_relationship_Add

To know this, let’s take an example of Two modules of SuiteCRM: Lead and Account. You added the relationship between both these modules. Here the Logic Hooks will execute each side of the relationship. If you edit lead details in Account module or do changes of Account details in Lead module, the hook will run for both. 

  • after_restore

It executes after your records gets undeleted.

  • before_restore

It executes before your records gets undeleted.

  • After_save

Here is an example of SuiteCRM Google Calendar Product. Suppose if you save any important Business meeting or event of your client in SuiteCRM, hook execute the action by saving directly on the Google Calendar in real-time. All team members get updates on the time. 

  • before_save

SuiteCRM Mailchimp Integration (Premium) product is the best example here. If you update a subscriber’s list in SuiteCRM software than before it updates the database of SuiteCRM, Hook executes the action on Mailchimp first. This Before_save performs in the field of Edit View. 

  • after_retrieve

Let’s understand this with the Extension of Twilio Click to call. Your call recordings are saved in your Database. Now you would like to see recordings in Detail View and in HTML Format. It retrieves all this from Database. All recordings will be visible in the Detail View. 

This After_retrieve performs in the field of Edit view and Detail view. 

  • process_record

For this, let us share an example of SuiteCRM Direct delete from Listview. A user performs an action to delete some unwanted records in the CRM. The Prcoess_record will see how many records are there in the field. After that, it adds the button on listview. 

This Process_record performs in the field of ListView, Sub-panel, Pop-up, and dashlet view

Application Level Hooks

The Third context executes the hooks when a user is working with the Global applications. This context is not recommended for the Modules or any Plugin. And it is located in

./custom/Extension/Application/EXT/LogicHooks/XYZ.PHP>

Types of Application Hooks are

  • after_entry_point
  • after_UI_Frame
  • after_UI_Footer
  • Server_round_trip: It executes when page loading ends and server successfully calculates loading time. 

For the Application Hooks, we have an example of Global Hide Manager (Premium) Plugin. When a user adds an action for hiding Element Class or ID, hooks will see what Element to hide in CRM software. Hooks execute this action on the event and hide all these from the Loaded page. 

How will you know the changes in Previous and New Values?

If it is necessary for your business team to know whether the change has occurred or not, use fetched_row. 

 if ($bean->fetched_row['{field}'] != $bean->{field})
{
    //logic
}

 

Another Example:

Checking few values for a field “status” and send email to anyone

 if($bean->status == “New” || $bean->status == “In Process”)
{
    //Code for Sending Email to anyone
}

 

Stay updated by signing up for our newsletter

Read our full Privacy Policy here.