Logic Hooks in SuiteCRM: A Comprehensive Developer’s Guide
Every Suite developer needs to work with Logic Hooks—they are amazing and truly a life saver.
It's tweaking. It's awesome!!
Read More: Logic Hooks in SuiteCRM: How to Create it?
Now it's time to learn more about SuiteCRM deeply. There are 2 types of hooks:
Types of Logic Hooks in SuiteCRM
1. Application-level Hooks
These hooks are best used in the following scenario:
after_ui_frame: After providing the UI frame
after_ui_footer: After providing the UI footer
server_roundtrip: During a server request-response cycle
before_logout: Before a user logs out
after_logout: After a user logs out
before_login: Before a user logs in
after_login: After a user logs in
login_failed: Where a login attempt fails
after_session_start: After a session starts
after_entry_point: After an entry point is accessed
2. Module Level Hooks
Best to use for :
after_save: Executes after saving a record
before_save: Executes before saving a record
before_retrieve: Executes before retrieving a record (triggered on EditView and DetailView)
after_retrieve: Executes after retrieving a record (triggered on EditView and DetailView)
process_record: Used for ListView, Popup View, and Subpanels.
before_delete: Executes before deleting a record
after_delete: Executes after deleting a record
before_restore: Executes before restoring a record
After_restore: Executes after restoring a record
Note: About Logic Hooks
- All logic hooks must have $hook_version and $hook_array variables defined.
- The following sections cover each required variable. Hooks should be placed in the Extension directory—it is advisable to use an extension-based hook structure.
Implementing Module Level Hook: Example
Let’s walk through an example where we automatically update the status of an opportunity to "Closed Won" if the amount is more than $500.
Step 1: Create a Logic Hook File:
Navigate to the following path
custom/Extension/modules/Opportunities/Ext/LogicHooks/my_win_opp.php
Put the following code into the file named my_win_opp.php
<?php
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
1,
'Store values',
'custom/modules/Opportunities/my_win_opp_win_sales.php',
'my_win_opp_class',
'my_win_opp_method'
);
?>
Step 2: Create the Hook Logic File
Now create a file in
custom/modules/Opportunities/my_win_opp_win_sales.php
Put this code:
$bean->sales_stage ='Closed Won';
Step 3: Run the Code for Quick Repair
- Go to Admin Panel
- Select repair, then quick repair and rebuild
Step 4: Test the Hook
- Create an opportunity
- Put an amount figure greater than 500
- Check the “sales stage” field—this should automatically change to "Closed Won."
Implementing Application Level Hook: Example
Now, it's time to make some application-level hooks—use the same strategy with a few changes.
Path for application hook:
custom/Extension/application/Ext/Logic Hooks
Key Differences:
- Application-level hooks will only use 2 variables—$event and $arguments.
- $bean is not available there as it is application level—since it does not apply for a specific module or record.
Final Thoughts
Logic hooks in SuiteCRM are extensively powerful, but some factors make differences, such as path structure, file names, and small syntax errors. By repeating the above steps correctly and structuring your hooks properly, you can enhance and automate SuiteCRM functionality with ease. However, the above process is simple but sometimes hard to learn paths, names, etc. Mistakes are obvious, and tiny mistakes are killing our time.
Go ahead and try it out today to tweak SuiteCRM and customise it exactly how you want.
Here is a much simpler solution. "Outright developer tools." It allows you to make all files, classes, and methods your own. Even more, it allows you to start/stop hooks without going into files. Moreover, you can start testing mode so you can build a hook that will work only on your test records.
It always seems impossible until it is done, so keep doing it!
Related Post: