How can i implement a logic hook

Hello, I want to show an error message when Invoice status becomes ‘validated’,
Here is my logic hook:

<?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, 'status invoices Changes', '/var/www/html/suitecrm/modules/AOS_Invoices/AOS_LogicHooks.php','AOS_LogicHooks', 'statusInvoicesChanges'); ?>

And this is my action class :

<?php class AOS_LogicHooks { public function statusInvoicesChanges (SugarBean $bean, $event, $arguments) { if ($dictionary['AOS_Invoices']['fields']['status'] == 'validated') { $GLOBALS['log']->debug("Status has been changed"); } } } ?>

So, when I open Admin>system settings>save the log. There is no display of my message. What i’m messing ?

Hi and welcome!

Please have a look here:

  • use the proper paths (very important)
  • debug your code -> will help you a lot

Also have a look at your log-level, maybe “debug” messages are not active yet.

1 Like

Yes my log level is active with “Debug”

Hi, @turik!
Here is your code. It’s more readable.
Look at line under my comment - ‘Error here’. You should delete ‘SugarBean’

<?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, 
      'status invoices Changes'
      '/var/www/html/suitecrm/modules/AOS_Invoices/AOS_LogicHooks.php','AOS_LogicHooks', 
      'statusInvoicesChanges'); 
?>

<?php 
class AOS_LogicHooks { 
/*                                       Error here */
  public function statusInvoicesChanges (SugarBean $bean, $event, $arguments) { 
    if ($dictionary['AOS_Invoices']['fields']['status'] == 'validated'){ 
      $GLOBALS['log']->debug("Status has been changed"); 
    }
  } 
} ?>
1 Like

Hi @p.konetskiy ! thank you for your reply
I deleted SugarBean and there is no display of my debug log.
This what I mean by status of invoices becomes ‘validated’

@p.konetskiy I’m not sure that event name ‘after_save’ is correct and also ($dictionary[‘AOS_Invoices’][‘fields’][‘status’] == ‘validated’).

@turik
May be problem in capital letter ‘v’ or ‘V’ in the word ‘validated’?

I fixed and there is no display of my message.

@turik
Next step. You use metadata:

$dictionary['AOS_Invoices']['fields']['status']

But you should use object:

$bean->status

I fixed with $dictionary[’AOS_Invoices’][’*fields’][$bean->status] == ‘Validated
And nothing happen

you should replace [’AOS_Invoices’][’*fields’][$bean->status] completely

if ($bean->status == 'validated'){ 
      $GLOBALS['log']->debug("Status has been changed"); 
}

the message is not being triggered by a status change though, its just active for status==‘validated’.

1 Like

@crmspace I try it
if ($bean->status == ‘validated’){
$GLOBALS[‘log’]->debug(“Status has been changed”);
}
And nothing happen.
I m gonna try to show you with pictures the steps:


Then admin=>system settings
Capture2
In the log file there is no message contains “status has been changed”

@turik
This is code for you:

<?php 
class AOS_LogicHooks { 
  public function statusInvoicesChanges ($bean, $event, $arguments) { 
    if ($bean->status == 'Validated'){ 
      $GLOBALS['log']->debug("Status has been changed"); 
    }
  } 
} 
?> 

Check capital letter for value ‘Validated’ or ‘validated’

1 Like

@p.konetskiy I try it and nothing happen. I think maybe the location of my logic hook and action class file is worng. i put it under the modules AOS_Invoices : /var/www/html/suitecrm/modules/AOS_Invoices

@turik
The hook should be here:
/var/www/html/suitecrm/custom/modules/AOS_Invoices
Documentation here:

1 Like

@p.konetskiy I fixe the location and nothing happen

event name “after_save” is that true ?

@turik
Please, change your code

<?php 
class AOS_LogicHooks { 
  public function statusInvoicesChanges ($bean, $event, $arguments) { 
// addition line:
$GLOBALS['log']->debug(get_class()." ". __FUNCTION__." Status:\n ".print_r($bean->status,true));
    if ($bean->status == 'Validated'){ 
      $GLOBALS['log']->debug("Status has been changed"); 
    }
  } 
} 
?>

It’s show that function called.

1 Like

Hi @turik,

Sorry to get into the conversation. I think you have to learn and understand the basic logic-hook structure and implementation before you embark in doing your own hooks. As @crmspace and @p.konetskiy point out before, you should start by reading the documentation: https://docs.suitecrm.com/developer/logic-hooks/

Now, here is a sample of the basic structure:

Other valid samples here:
https://7thzero.com/blog/add-logic-hook-case-generation-sugarcrm-community-edition

Thanks,

BrozTechnologies

2 Likes

@BrozTechnologies Thank for your help,
the problem was solved, i had to change the name of my hook file to “logic_hooks” and it works. special thank to @p.konetskiy and @crmspace