$GLOBALS["db"]->query()  not working in before save logic hook

Hi

  I want to execute an sql statement in the before save logic hook.

I have tried

$GLOBALS['db']->query("UPDATE users SET description='test' WHERE id='27283e5a-8f34-b846-367a-561fbbb4cb54'");

$db = DBManagerFactory::getInstance();

$res = $db->query("UPDATE users SET description='test' WHERE id='27283e5a-8f34-b846-367a-561fbbb4cb54'");

But it is not working.Shijin Krishna@

  • Please make sure your logic hook file is getting called,by printing something to log.

  • The bean you're changing is it the same that triggers the hook? If it's not I would not use an before save hook but an after instead. If it is you can't save it, as I sad earlier.

         function before_save_method($bean, $event, $arguments){

                        $bean->description = 'Test';

          }

  • Hi Ajay

         Actually i want to execute an sql query inside the logic hook as i start up i am  using the above query.The code which you have mentioned a above is working for me but need to run an sql query inside the logichook

  • Hi Gustav

         Actually i want to execute an sql query inside the logic hook as a start up i am  using the above query as a sample.The code($bean->description="Test") is updating the description field.But i want an sql query to get executed in side the logic hook.

  • Ok.Have you tried like below,

    global $db;

    $res = $db->query("UPDATE contacts SET description='test' WHERE id='102162e1-62cd-1fa3-1bcf-556fb62dc90b'");

  • Okay, try the exact same thing you did initially, but with an after save hook. Also do a log call to make sure it running.

    $GLOBALS['log']->fatal('Database update');

  • Hi Ajay

        Yes i have tried.But the sql statement was not updating.

  • Is it throwing any error to log?

  • It will be easy to figure it out the issue,if you paste the code here.

  • Hi Ajay

        This is the code which i am using for executing an sql statement in the before save logic hook

    <>/custom/modules/Users/logic_hooks_class.php

    $hook_version = 1;

    $hook_array = array();

    $hook_array['before_save'] = array();

    $hook_array['before_save'][] = Array(

            //Processing index. For sorting the array.

            1,

            'before_save example',

            'custom/modules/Users/logic_hooks_class.php',

            'logic_hooks_class',

            'before_save_method'

        );

    In

    <>/custom/modules/Users/logic_hooks.php

    <?php

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

    class logic_hooks_class{

         function before_save_method($bean, $event, $arguments){

              global $db;

              $res = $db->query("UPDATE users SET description='test' WHERE id='27283e5a-8f34-b846-367a-561fbbb4cb54'");

              }

    }

1 2 3