Sugar 7 - Prevent certain records from being deleted

Hi, in sugar 6, I could prevent a record being deleted by doing this in custom/modules/Accounts/controller.php:

<?php

if (!defined('sugarEntry'))
    define('sugarEntry', true);

require_once 'include/MVC/Controller/SugarController.php';

class AccountsController extends SugarController {

    function action_delete() {
        if (empty($this->bean->some_field_c)) {
            parent::action_delete();
        }

        return;
    }
}

So the account would be deleted as long as some_field_c was empty. How can I do the same thing in sugar 7? It looks like custom/modules/Accounts/controller.php is no longer read. I know I can edit the record.js for accounts and catch the delete trigger and stop it there, but this is client side, plus I would need to do it for the recordlist view, massupdate view, etc. It would also mean that other apps accessing the sugar API delete these records.

I also tried the before_delete logic hook which does get called but you can't intercept the delete here.

Thanks.

  • Hi John,

    The easiest way that comes to mind would be to override the API Endpoint for delete on that module, adding your logic in that function. Let me know if that helps or if you have any questions on it!

    -Alan

  • Thanks for that. I overrode the deleteRecord() method from the ModuleAPI class and it works. I guess the only other thing to add would be to make it say an error ("Can't delete this account") or something but at least I can now prevent accounts from being deleted.

    Also I noticed that if a module is ran in backward compatibility mode then it does read custom/modules/<module>/controller.php but not if the module is running in sugar 7 mode.

    Thanks for the help.

  • Could you please share steps you followed to override deleteRecord() method. I am trying same but getting 500 error on delete of record. Steps I followed:

    Custom file created at location: /var/www/html/habasit820/custom/modules/<Module>/clients/base/api/<file_name>.php

    Code:


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

    require_once("clients/base/api/ModuleApi.php");

    class PreventDelete extends ModuleApi
    {
    public function registerApiRest()
    {
    return parent::registerApiRest();
    }

    public function deleteRecord(ServiceBase $api, array $args)
    {
    $this->requireArgs($args,array('module','record'));

    // Users can be deleted only in cloud console for IDM mode.
    if (in_array($args['module'], $this->idmModeDisabledModules)
    && $this->isIDMModeEnabled()
    && empty($args['skip_idm_mode_restrictions'])) {
    throw new SugarApiExceptionNotAuthorized();
    }

    $bean = $this->loadBean($api, $args, 'delete', $this->aclCheckOptions);
     if($bean->custom_field == '') {
     $bean->mark_deleted($args['record']);

    return array('id'=>$bean->id);
     }
    }

    Quick Repair and Rebuild operation is performed. 

    On delete of record, I am getting 500 error.

    Kindly suggest where I am going wrong.

    Also I have to show alert in else condition. How can we do that?