Make a quick vote 🗳 ️How do you refer to SuiteCRM extensions: add-ons or plug-ins? Click here to have your say 👈

Delete Logic Hook

It should work, but it doesn’t.
Thanks for your help

Before Delete Logic Hook
For this new scenario, the requirement is when an Account is deleted, all the contacts related to this accounts need to be deleted. In this ‘before_delete’ logic the code should fetch all the contact beans related to this account and mark the contacts as deleted.

Steps to create before delete hook
Logic needs to be triggered when an account is deleted. Create a logic hook in the below path.
./custom/Extension/modules/Accounts/Ext/LogicHooks/delete_contacts.php

<?php
   $hook_array['before_delete'][] = Array(
        1, 
        'Delete related contact records',
        'custom/modules/Accounts/delete_contacts.php',
        'delete_contacts',
        'delete_related_contacts' 
    );
?>

Create a logic hook class in the below path
./custom/modules/Accounts/delete_contacts.php

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

class delete_contacts {

    function delete_related_contacts($bean, $event, $arguments) {
      if($bean->load_relationship('Contacts')){
        $arr_conIds[] = $bean->Contacts->get();
        foreach ($arr_conIds as $key => $id) {
           $contact_bean = BeanFactory::registerBean($id);
           $contact_bean->mark_deleted($contact_bean->id);	
           $contact_bean->save();
	    }
      }
    }
}
?>