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

  • 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 Anand Chitikela,

    Have you tried like global $db; to get $db instead of creating db instance.

    Can you please try below two possibilities

    1.

    global $db;

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

    2.

    require_once('data/BeanFactory.php');

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

    $user->description = 'Test';

    $user->save();

    Thanks!.

  • If it's an before save hook and it's the same record/bean that jst got saved he can't do a save bean, that will create a infinite loop. He should do it the way Roman adviced, change bean that is handed with the function.

  • Hi Roman and Gustav

         I am currently work in on sugarcrm version 7.6.I have used

    class logic_hooks_class{

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

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

                        $user->description = 'Test';

                        $user->save();//line 8

                }

    But this was throwing

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

  • Hi Ajay

        I have tried the above ways but it was not doing my work.I am currently work in on sugarcrm version 7.6

  • Hi roman roman

        Actually i want to execute an sql query inside the logic hook as i start up i am  using the above query.

  • Hello Anand Chitikela,

    I tested below code in contacts module sugar ent 7.6.1.0.It is working for me.

    /crm/custom/Extension/modules/Contacts/Ext/LogicHooks/logic_hooks.php

    <?php

        $hook_version = 1;

        $hook_array = array();

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

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

            1,

            'test',

            'custom/modules/Contacts/customLogicHook.php',

            'TestClass',

            'TestFunction'

        );

    ?>

    /crm/custom/modules/Contacts/customLogicHook.php

    <?php

    if(!defined('sugarEntry')) die('Not a valid entry point');

        class TestClass

        {

            function TestFunction($bean, $event, $arguments)

            {

                $bean->phone_mobile='556677';

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

            }

        }

    ?>

1 2 3