Abort Delete event through logic hook

Hi Suite Forums :cheer:
I have been working with logic hooks and I’m trying to figure out how to abort/cancel deleting a record under certain conditions.

from the looks of it, deleted isn’t set until after the before_delete event

so I have attempted to set deleted to 0 during the after_delete event
after the logic hook has ran though, the record is absent from the listview
and inserting the record uuid into the url parameters results in a blank ‘content’ area

also there are no logs to explain the blank page

I know that flipping deleted from 1 to 0 directly in the database with sql or whatever query language (I think mySQL is the only supported DBMS though) brings the record back so I’m a little confused into what’s going on

do I have to directly use sql instead of the bean object to achieve the desired result?

You are trying to access a deleted Record? Can you explain the whole scenario?

When a user deletes a record, this also fires two events:
before delete and after delete.

During these events and others like them, you can run custom scripts.
My script can check a record/bean for certain values during such events.

I want to be able to use this ability to determine if a record should be deleted or not,
so lets say don’t delete records with a status of completed,

when a user deletes a record with such a status,
this custom script will match that value with an action.

and this action would be to prevent the delete from happening.

There does not seem to be a way of cancelling the delete action in the before delete event so I have tried to set the value of deleted to 0 during the after delete event.

Doing this though, the record still does not show up in the list view and when manually entering it’s UUID into the url, you are redirected to a blank page.

I’ve seen this come up in the forums before, I don’t think anybody had an answer for it.

I agree it would be useful and make sense, but hooks don’t seem to allow for a cancellation mechanism (typically it would be triggered by returning false)…

Recently ran into this and here’s the solution which worked for me, hopefully this doesn’t break anything, but it looks pretty safe

In data/SugarBean.php

Inside the function mark_deleted()

Right under the following line:
$this->call_custom_logic("before_delete", $custom_logic_arguments);

You would add the following:

    if(isset($this->abort_mark_deleted)) {
        unset($this->abort_mark_deleted);
        return;
    }

Then you can stop the deletion of the record by declaring

	$bean->abort_mark_deleted = 1;

Inside your before_delete hook

Unfortunately this is not upgrade safe, so you would need to mind this, but it’s the only way to go. Even doing $bean->mark_undeleted($id) does not help because it does not recreate a previously deleted relationships which get destroyed when a record is deleted and even if it did it could potentially fire up another logic hook for when the relationship is recreated which could cause problems. So this solution is probably the best way to go.

1 Like

Thanks Robert!

If you have the experience that that works well, and doesn’t have any unintended side-effects, I would just make a PR and put that into core. It’s low-risk, since only people who use that flag will actually run any new code.

1 Like

I’ve done a bunch of testing with this and so far it didn’t cause any problems. Just made a PR

1 Like

robert_sinclair = ildarius?

1 Like

yep that’s the one :slight_smile:

robert_sinclair = ildarius = Rutger Hauer = Roy Batty from Blade Runner ?

:confused:

:laughing:

1 Like

By the way - about your latest video (keep them coming, they’re great!), you mention the problems with that change not being upgrade-safe, but did you know that the Extension mechanism has a possibility to extend Menu.php, that should probably be used and it will get picked up here. Although I never tried it and I am not sure how to do it.

1 Like

lol, all 3! :slightly_smiling_face:

Glad you liked the videos, I have a lot more planned in the future. May be next one will be on how to arrange documentation when dealing with large projects, that should be helpful to people who’re dealing with something a bit more complex.

I’ll have to check out the extension mechanism, thanks!

1 Like