How can i implement a logic hook

@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

It works but after that i found that it fires whenever he find

status=“validated”

Whereas in my example I want it to trigger only when status changes

“x” status to “Validated”

Here is an example:
Capture5

Capture6

@turik
The variable has before value:

$bean->fetched_row->status

You can change the code in the following way

if ($bean->status == 'validated' && $bean->fetched_row->status <> $bean->status)
1 Like

@p.konetskiy I changed my code.
In this example it is triggered even if I modify the date and not the status :

@turik
Please, show your code.

@p.konetskiy This is my logic hooks:
<?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’, ‘custom/modules/AOS_Invoices/statusChange.php’,‘statusChange’, ‘statusInvoicesChanges’);
?>
my class :
<?php
class statusChange {
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’ && $bean->fetched_row->status <> $bean->status){
$GLOBALS[‘log’]-> debug(“Status has been changed”);
}
}
}
?>

@turik
I am sorry. I forgot that ‘fetched_row’ is array but not object. Please, change:

  • from:
$bean->fetched_row->status
  • to:
$bean->fetched_row['status']
1 Like

@p.konetskiy It works thanks.
I am moving forward in the work and needs can be change

How can i get ID of the invoice processed with my logic hook ?

Hey,
you should set up your IDE so that you’r able to debug your code, it will definitely help you a lot (and you’re probably looking for $bean->id).

2 Likes

There is a value in variable:

$bean->id

You can show all information about object but I recommend switch log level from ‘debug’ to 'fatal:

$GLOBALS[‘log’]->fatal(get_class()." “. FUNCTION.” Status:\n ".print_r($bean,true));

Look at documentation:


1 Like

Hi i want my logic hook fires when status =“paid” AND when date of payment change like this example :


So i did this and nothing happen

if($bean->status == ‘Paid’ && $bean->date_payment_c <> $bean->fetched_row[‘date_payment_c’]){}

what i’m missing ?

@turik
Now I sure that you know how to see values of variables in log file. You should print to log the variables and analyse.

1 Like

@p.konetskiy! Thanks i found the solution