Logic Hook - not being triggered

Hello,

I am trying to trigger a logic hook as follows in a custom built module named: user_account but unfortunately is not working.
What am i doing wrong?

hook.php at the path: /var/www/html/SCRM/custom/modules/user_account

<?php
$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1,'AutoFill Custom Field','custom/modules/user_account/LogicHooksClassTest.php','LogicHooksClassTest','autoFillCustomField');

and the LogicHooksClassTest.php at the same path: /var/www/html/SCRM/custom/modules/user_account :

<?php
class LogicHooksClassTest
{
    function autoFillCustomField($bean, $event, $arguments)
    {
        $bean->customer_cui = $bean->customer_code_crs;
    }
}

Both the fields customer_cui and customer_code_crs are defined as integers in user_account module.
SuiteCRM version is Version 7.14.3
Sugar Version 6.5.25 (Build 344)

I was not able to find any logging regarding the hook.
Best regards,

Mihai

Hi, welcome to the Community! :tada:

You must follow the documentation precisely.

In this case I believe your problem is the file name, it has to be logic_hooks.php

unfortunately is still not working. of course i repaired and built again, and i also cleared the browser cache after each new saving
it should be something very simple (my intention is to be able to fill in fields with some api values in the end…but it looks like i am not able to manage this :slight_smile: ) I tried to make a print screen with all the relevant info.
Regards,

Mihai

First, you should make sure that the file is getting picked up. You could add some code in the logic_hook.php file to leave some logging or write some file on the disk, so you could make sure it was executed.

Then, if the hook gets correctly defined in the hook array, the next step is to check if the function call happens: put some logging there at the beginning.

Only after you are sure of that, try changing the bean.

Hello,
I kept trying to do some logging but unfortunately i haven’t succeeded.
What i have done:

  1. Given permissions to the files:
    root@contractservicingprocessor:/var/www/html/SCRM/custom/modules/user_account# ls -l
    total 12
    -rw-r–r-- 1 www-data www-data 1691 Apr 15 11:51 LogicHooksClass.php
    -rw-r–r-- 1 www-data www-data 657 Apr 15 14:32 LogicHooksClassTest.php

  2. modified the hook as per below:

<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

$hook_version = 1;
$hook_array = Array();
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(
    1,                              // Sorting order, lower numbers are processed first
    'Test AutoFill Custom Field',   // Description of the hook for identification
    'custom/modules/user_account/LogicHooksClassTest.php', // Path to the logic hook class file
    'LogicHooksClassTest',          // Name of the class containing the hook method
    'autoFillCustomField'           // Name of the method to be called
);

// Optional: Debugging line to check file loading
file_put_contents('custom/modules/user_account/logic_hook_loaded.log', "logic_hooks.php loaded at " . date('Y-m-d H:i:s') . "\n", FILE_APPEND);
  1. modified the hook function as per below just to be sure everything is ok from the syntax pov (test1 field is a text field):
<?php

class LogicHooksClassTest
{
    public function autoFillCustomField($bean, $event, $arguments)
    {
        // Basic logging to SuiteCRM log file to confirm execution
        global $log;
        $log->fatal("Hook executed: Entered autoFillCustomField method for bean ID: {$bean->id}");

        // Simple operation for testing: updating a field with a static value
        $bean->test1 = 'Test value set by logic hook';  // Ensure 'some_custom_field_c' exists in your module

        // Optional: Direct PHP logging for troubleshooting
        error_log("Hook executed for record {$bean->id} at " . date('Y-m-d H:i:s'));
    }
}
  1. the path for the hook and hook function is: /var/www/html/SCRM/custom/module/user_account (the installation directory is SCRM and the cusotm module’s folder is user_account)

There is still no logging neither at suitecrm level nor php…

if you’d have any other ideas i would very much like to hear them.

best regards,

mihai

Please enclose your code blocks in triple back-ticks, to get proper formatting here in the forums:

```php
code
```

When you run a Quick Repair & Rebuild, do you get any logging or any contents from that file_put_contents?

i run quick repair and build but it generates no file

I would try removing all your files from that directory except logic:_hooks.php.

Just put them somewhere else and adjust the references inside the code.

Are you using an IDE with XDebug, so you can place breakpoints and see where the code is going?

I left only the hook in the user_account folder as you suggested.
This is what i get in the suitecrm.log:

Mon Apr 15 21:17:50 2024 [3200500][1][DEBUG] Performing action: action_save MODULE: cprd_user_account
Mon Apr 15 21:17:50 2024 [3200500][1][DEBUG] Hook called: cprd_user_account::before_save
Mon Apr 15 21:17:50 2024 [3200500][1][INFO] Query:INSERT INTO cprd_user_account (id,name,date_entered,date_modified,modified_user_id,created_by,deleted,customer_segment,customer_cui,customer_code_crs,test1,test2)
VALUES (‘7d8fdad5-b2af-906a-e880-661d6f77f9be’,‘’,‘2024-04-15 18:17:50’,‘2024-04-15 18:17:50’,‘1’,‘1’,0,‘Micro’,NULL,6,‘’,‘’)
It seems that the hook is called now but it doesn’t trigger the hook function.
There is no other log file written or updated.
What i noticed is that in the log the module is cprd_user_account which is the “package key_module name” and not the module name. This is also the name i see in the DB for the corresponding table.

I think you have found your issue the name of your module for all backend purposes is cprd_user_account

so the path you wrote above should be
/var/www/html/SCRM/custom/modules/cprd_user_account

similarly at other places as well where you have used the name of module

1 Like

i have one more question: the before_save hook is executing the logic from the hook function also in the UI before saving? In my case the value i have in the field should be completed real time in the UI?

No. Logic hooks run in the back end, you won’t see any effect on the UI unless you refresh the browser.

Thank you for your help and patience @pgr and @abuzarfaris !

1 Like