AnsweredAssumed Answered

Cannot use 2 Logic Hooks in same Class, only separately

Question asked by Ramon Marcondes on Feb 10, 2017

Hello.

I'm using Sugar Enterprise 7.8.

I have the following problem:

I need two logic hooks for one module, a 'before_save' hook to get some bean values before saving, and an 'after_save' hook to get the these previous values.

I checked this tutorial:

Doing before/after field comparisons in after_save logic hooks « Sugar Developer Blog – SugarCRM 

 

But what happens to me is:

The 'after_save' hook is called before the 'before_save hook' (???), and the 'after_save' hook bean comes empty (???)

The only way the logic hook order worked was separating the hooks in different files and classes.

 

This is the code that doesn't work, with two hooks in a single class:

// manifest.php
'logic_hooks' => array(
         array(
                'module' => 'Opportunities',
                'hook' => 'before_save',
                'order' => 50,
                'description' => 'Before',
                'file' => 'custom/modules/Opportunities/add_code_hook.php',
                'class' => 'add_code',
                'function' => 'before_code',
         ),
     array(
                'module' => 'Opportunities',
                'hook' => 'after_save',
                'order' => 99,
                'description' => 'After',
                'file' => 'custom/modules/Opportunities/add_code_hook.php',
                'class' => 'add_code',
                'function' => 'after_code',
     ),
);

// add_code_hook.php
class add_code {
     function before_code($bean, $event, $arguments) {
          $GLOBALS['log']->fatal("Before code");
     }
     function after_code($bean, $event, $arguments) {
          $GLOBALS['log']->fatal("After code");
     }
}

 

And this is the code that works, hooks in separate files and classes:

// manifest.php
'logic_hooks' => array(
         array(
                'module' => 'Opportunities',
                'hook' => 'before_save',
                'order' => 50,
                'description' => 'Before',
                'file' => 'custom/modules/Opportunities/add_code_hook_before.php',
                'class' => 'add_code_before',
                'function' => 'before_code',
         ),
     array(
                'module' => 'Opportunities',
                'hook' => 'after_save',
                'order' => 99,
                'description' => 'After',
                'file' => 'custom/modules/Opportunities/add_code_hook_after.php',
                'class' => 'add_code_after',
                'function' => 'after_code',
     ),
);

// add_code_hook_before.php
class add_code_before {
     function before_code($bean, $event, $arguments) {
          $GLOBALS['log']->fatal("Before code");
     }
}

// add_code_hook_after.php
class add_code_after {
     function after_code($bean, $event, $arguments) {
          $GLOBALS['log']->fatal("After code");
     }
}

 

Can someone tell me why 2 hooks in the same class doesn't work properly, but separetely works?

Any help would be appreciated.

 

Thanks!

Best Regards,

Ramon

Outcomes