$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
  • If you use before save logic hook in module Users, Sugar logic overwrites field [Description].

    You need use this:

    function customHook(&$focus, $event, $arguments){
        $focus->description = 'test';
    }
    
  • Update the bean instead

    $user= BeanFactory::getBean('users', '27283e5a-8f34-b846-367a-561fbbb4cb54');

    $user->description = 'Test';

  • Hi Gustav Lindstrom

        I have tried

    $user= BeanFactory::getBean('users', '27283e5a-8f34-b846-367a-561fbbb4cb54');

    $user->description = 'Test';

        but it wasnt working.

  • You also need this:

    $user->save();

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

              }

    }

  • Hi Gustav

    The crm is throwing this error when we save

    Fatal error: Call to undefined method stdClass::save() in /<>/custom/modules/Users/logic_hooks_class.php

  • 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!.

  • Didn't you want to do an database update?

    If the method don't exist you might need to add the following line.

    require_once 'modules/Users/User.php';

Reply Children
No Data