Can't get logic hook to trigger, please help!

Hello all! I recently stood up a SuiteCRM 8.9.1 server and have received lots of customization requests. Many of which have been achievable through the workflow manager, but this seems like it might need a logic hook and even after reading the section on logic hooks in the dev docs, I can’t seem to get the hook to ever fire. Any help very much appreciated!

/var/www/suitecrm/public/legacy/custom/modules/logic_hooks.php

<?php 
// Do not store anything in this file that is not part of the array or the hook version.  This file will // be automatically rebuilt in the future.  
$hook_version = 1; 
$hook_array = Array(); 
// position, file, function 
$hook_array\['after_save'\] = Array(); 
$hook_array\['after_save'\]\[\] = Array(1, 'ElasticSearch Index Changes', 'lib/Search/ElasticSearch/ElasticSearchHooks.php','SuiteCRM\\Search\\ElasticSearch\\ElasticSearchHooks', 'beanSaved'); 
$hook_array\['after_save'\]\[\] = Array(1, 'addCompanyToTask', '/var/www/suitecrm/public/legacy/custom/modules/Tasks/copyCompany.php', 'CopyCompany', 'copyFromLead'); 
$hook_array\['after_save'\]\[\] = Array(30, 'popup_select', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_select');
$hook_array\['after_delete'\] = Array(); 
$hook_array\['after_delete'\]\[\] = Array(1, 'ElasticSearch Index Changes', 'lib/Search/ElasticSearch/ElasticSearchHooks.php','SuiteCRM\\Search\\ElasticSearch\\ElasticSearchHooks', 'beanDeleted'); 
$hook_array\['after_ui_footer'\] = Array(); 
$hook_array\['after_ui_footer'\]\[\] = Array(10, 'popup_onload', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'popup_onload');
$hook_array\['after_ui_frame'\] = Array(); 
$hook_array\['after_ui_frame'\]\[\] = Array(20, 'mass_assign', 'modules/SecurityGroups/AssignGroups.php','AssignGroups', 'mass_assign'); 
$hook_array\['after_ui_frame'\]\[\] = Array(1, 'Load Social JS', 'include/social/hooks.php','hooks', 'load_js');  
?>   

/var/www/suitecrm/public/legacy/custom/modules/Tasks/logic_hooks.php

<?php  
// Do not store anything in this file that is not part of the array or the hook version.  
$hook_version = 1; 
$hook_array = Array(); 
$hook_array\['after_save'\] = Array(); 
$hook_array\['after_save'\]\[\] = Array(1, 'Copy company from related Lead', 'custom/modules/Tasks/TaskCompanyHook.php', 'TaskCompanyHook', 'copyCompany');  
?>   

/var/www/suitecrm/public/legacy/custom/modules/Tasks/TaskCompanyHook.php

class TaskCompanyHook 
{
    public function copyCompany($bean, $event, $arguments)
     {         
         file_put_contents('/tmp/task_hook.log', date('Y-m-d H:i:s') . " HOOK FIRED\\n", FILE_APPEND);
         file_put_contents('/tmp/task_hook.log', "parent_type: " . $bean->parent_type . "\\n",                         FILE_APPEND);
         file_put_contents('/tmp/task_hook.log', "parent_id: " . $bean->parent_id . "\\n\\n",             FILE_APPEND         );
         if (in_array($bean->parent_type, array('Leads', 'Lead')) && !empty($bean->parent_id))
              { 
                   $lead = BeanFactory::getBean('Leads', $bean->parent_id);
                    if (!empty($lead->id))
                    {
                         $bean->company_c = $lead->account_name;
                    }
               }
          } 
}  

This gets auto generated after rebuild and repair

/var/www/suitecrm/public/legacy/custom/modules/Tasks/Ext/LogicHooks/logichooks.ext.php

<?php  
//WARNING: The contents of this file are auto-generated
$hook_array\['after_save'\]\[\] = array( 1,  'Copy company from related Lead', 'custom/modules/Tasks/TaskCompanyHook.php', 'TaskCompanyHook', 'copyCompany' );  
?>

I would do your logging like this to confirm its working:

$GLOBALS[‘log’]->fatal(‘TaskCompanyHook fired’);
$GLOBALS[‘log’]->fatal('parent_type: ’ . $bean->parent_type);
$GLOBALS[‘log’]->fatal('parent_id: ’ . $bean->parent_id);

Also generally you do not want an after_save hook. These create endless loops unless you deal with that in your code ie: if it already ran, don’t run again other wise after save will fire, update the record and then fire again and update the record……

I would try a before_save unless you really need after save.

If this hook applies to the tasks module, you want to fire it here:

**custom/modules/Tasks/Logic_Hooks.php

You do not put module hooks here:**
/var/www/suitecrm/public/legacy/custom/modules/logic_hooks.php

That is for application hooks only.

1 Like

The file you have listed above is in the wrong place. It should be here:

/var/www/suitecrm/public/legacy/custom/Extension/modules/Tasks/Ext/LogicHooks/TaskCompanyHook.php

Contents:

<?php  
// Do not store anything in this file that is not part of the array or the hook version.  
$hook_version = 1; 
$hook_array = Array(); 
$hook_array['after_save'] = Array(); 
$hook_array['after_save'][] = Array(1, 'Copy company from related Lead', 'custom/modules/Tasks/TaskCompanyHook.php', 'TaskCompanyHook', 'copyCompany');  

I would repeat @pstevens that it should probably be a before_save for safety sake.

Also these bits:

$hook_array = Array(); 
$hook_array['after_save'] = Array(); 

would need testing for existence first as you could wipe out other after_save arrays.

Mark

1 Like

Thank you for your help, this makes a lot of sense. I reverted the changes I was trying to make when none of it worked and will be trying it this way soon

Thank you, Mark. This is a lapse in knowledge I hit for sure. So am I understanding correctly that, when adding module-specific logic hooks, I first need to make sure there is not already an “after_save” (in this example) array in the main logic hooks array at the application level? How would you yourself go about safely implementing this properly / checking for existence? Thanks!

@chase

if(!isset($hook_array)){
$hook_array=array():
}
if(!isset($hook_array['after_save'])){
$hook_array['after_save']=array();
}

There are shortened versions but you get the idea.

Mark