Published by  Ashish Dwivedi  on

SuiteCRM Dev Journey : All about Logic hooks

Every Suite developer needs to work with Logic hooks , they are amazing, they are life saver.

It’s tweaking, It’s awesome!!

Now it’s high time to learn more about SuiteCRM deeply. There are 2 types of Hooks.

  • Application Level
  • Module Level
  1. Application level ->  Best to use in : after_ui_frame ,after_ui_footer ,server_roundtrip ,before_logout ,after_logout ,before_login , after_login ,login_failed , after_session_start , after_entry_point
  2. Module Level ->   Best to use for :
    * after_save   -> After saving any record.
    * before_save -> Before Saving any record
    * before_retrieve  -> Before retrieving any record, This hooks calling on editview and DetailView
    * after_retrieve -> Before retrieving any record, This hooks calling on editview and DetailView.
    * process_record -> This hooks is for ListView , Popup View and Subpanels.
    * before_delete ->
    * after_delete
    * before_restore
    * after_restore
    All logic hooks must have a $hook_version and $hook_array variable defined. The following sections cover each required variable.Hooks should be places from Extension, it is advisable to use extension based hook structure.
  3.  Let us have an example of saving an Opportunity and if amount is more than 500$ , let us change it’s status to “Closed Won”.
  4. Drop  a file to custom/Extension/modules/Opportunities/Ext/LogicHooks/my_win_opp.php
  5. Put this code.    <?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’
    );
  6. Now create a file custom/modules/Opportunities/my_win_opp_win_sales.php
    1. Put this code<?php class my_win_opp_class{function my_win_opp_method($bean,$event,$arguments){
      if($bean->amount >500){
      $bean->sales_stage =’Closed Won’;
      }
      }
    2.  Go to admin and have a quick repair.
    3.  Go and have some fun, create an opportunity, put an amount figure more than 500, look at sales stage field, this should automatically change to “Closed Won”.
  7. Now it’s time to make some application level hook , use same strategy with few changes.
  8. Path of application hook will be custom/Extension/application/Ext/Logic Hooks
  9. Calling method will be only having 2 variable $event,$arguments.  $bean is not available there as it is application level ,loading not for a module or record.
  10. Go and replicate above steps for application-level hook. just keep in mind for their path and calling method parameters.

However above process is simple but sometimes hard to learn path, names etc. Mistakes are obvious and tiny mistakes killing our time.

Here is much simpler solution. “Outright developer tools”, It gives you an opportunity to make all files, classes/methods in their own. Even more, it gives you an opportunity to start / stop hooks without going into files. Moreover you can start Testing mode , so you can build a hook which will work only on your test records.

It always seems impossible until it is done, So keep doing!

Seems fun , Have a look at this video!

 

 


Leave a Reply

Your email address will not be published. Required fields are marked *