$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@

Parents Reply Children
  • 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'");

              }

    }

  • You have defined wrong path in logic hook definition file.

    And

    Here is the code snippet to update description field.

    Create a file in below path

    /crm/custom/Extension/modules/Users/Ext/LogicHooks/logic_hooks_class.php

    and content is like below,

    <?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/UpdateDescription.php',

            'update_Description_Class',

            'update_Description_Method'

        );

    Create file in below path

    /custom/modules/Users/UpdateDescription.php

    and content is

    <?php

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

    class update_Description_Class{

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

              global $db;

                $GLOBALS['log']->fatal("test before logic hook called");    

              $res = $db->query("UPDATE users SET description='ajaytest' WHERE id='3a949349-8d59-a91d-85c8-55a5de863611'");    

              }

    }

    This is working .

    Thanks!.