How to test after_restore logic hook action in SugarCRM v7.6.2 Ent Edition?

Dear Sugar Experts,

I have deleted an account record from my local SugarCRM.

I have the below logic hook code for Accounts module:

Logic Hook file location: /custom/modules/Accounts/logic_hooks.php

<?php

$hook_version = 1;

$hook_array = array();

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

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

       1,

       'after_restore example',

       'custom/modules/Accounts/AccountsLogicHook.php',

       'AccountsLogicHook',

       'restoreData'

   );

?>

Logic Hook file location: /custom/modules/Accounts/AccountsLogicHook.php

<?php

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

class AccountsLogicHook {

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

$GLOBALS['log']->dale("KKKKK" . print_r($bean, true));

$GLOBALS['log']->dale("KKKKK" . print_r($arguments, true));

}

}

?>

How to trigger the after_restore action (i.e., how to undelete an existing deleted record)?

I have executed the below query in MySQL command line:

UPDATE accounts SET deleted='0' WHERE (id='<account id>' AND deleted='1');

No action triggered. Please help out.

Thanks.

-Uday

Alex Nassi

  • Hi Uday,

    According to ~line 5542 of data/SugarBean.php, this is used in the mark_undeleted() function that is called if $_SESSION['show_deleted'] is set and a record is deleted. I haven't tested this yet, but you could probably write a customization to set this session variable to 1, perform a delete on a record, then unset the session variable. This would theoretically undelete records and fire the after_restore logic hook. I hope that helps!

    -Alan

  • Hi Alan,

    Thanks for your hint. I have triggered the after_restore logic hook action as below:

    Logic Hook file location: /custom/modules/Accounts/logic_hooks.php

    <?php

      $hook_version = 1;

      $hook_array = Array();

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

    $hook_array['after_delete'][]=array(1, 'after_delete example', 'custom/modules/Accounts/AccountsLogicHook.php', 'AccountsLogicHook', 'deleteData');

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

    $hook_array['after_restore'][]=array(1,'after_restore example', 'custom/modules/Accounts/AccountsLogicHook.php', 'AccountsLogicHook', 'restoreData');

    ?>

    Logic Hook file location: /custom/modules/Accounts/AccountsLogicHook.php

    <?php

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

    class AccountsLogicHook {

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

    $bean->mark_undeleted($bean->id);

    $GLOBALS['log']->dale("Undeleted after_delete action: " . $bean->id);

    }

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

    $GLOBALS['log']->dale("after_restore bean: " . $bean->id);

    $GLOBALS['log']->dale("after_restore arguments: " . $arguments['id']);

    }

    }

    ?>

    Now I could see the log in the sugarcrm.log for both after_delete and after_restore actions.

    Thanks again.

    -Uday

    Robert Bers (Sample code to trigger after_delete and after_restore)