Delete record and all related

Hello members!

I’ve been searching but couldn’t find a solution.
Maybe someone here can help me.

The general idea is to have all related records deleted when I delete the master record.
The specifics:

  • I created two custom modules and related them with a one-to-many relationship (module Configs and module items).
  • What I want is to delete all items records when I delete the related Configs record.

For example, is the same thing as if I wanted to delete all contacts from a given Account when I delete that specific Account.

Is there any option to do that ? Or it needs complicated coding ?

Thanks for your help

I’m afraid I can’t think of an easy way to automate this without writing code. However, if you are deleting an account manually then it should be fairly quick to go to the contacts subpanel of the account, select all and delete before you delete the actual account.

If you do want to automate it, then it woludn’t be very difficult to do using a logic hook if you have any experience of coding and customising SuiteCRM.

I want to minimize user error dependency.
But maybe I can create a before_delete logic hook with an SQL query and execute a delete record on the query result.
Any idea on how I can delete ‘by the book’ a record this way ?

I do this with a before_delete logic hook

$actions = $bean->get_linked_beans ( 'zp_action', 'zp_action' );
		
		foreach ( $actions as $action ) {
			$action->deleted = true;
			$action->Save ();
		}

I use the method mentioned by mikesolomon. However…if there is a relationship table (ie: relationships added in Studio) the relationship table record is not marked as deleted. I don’t believe this has any effect on SuiteCRM except for bloating the DB as these records wouldn’t get cleaned up.

Looking deeper…this is NOT the way to delete a record. I couldn’t figure out why my before_delete logic hook wasn’t being triggered. The only way the before_delete and after_delete logic hooks will be called is by directly calling the mark_deleted() bean method. When the “deleted” field is marked as true, the logic hooks aren’t fired and the relationships are left alone.

I believe a better solution would be:


$actions = $bean->get_linked_beans ( 'zp_action', 'zp_action' );
foreach ( $actions as $action ) {
	$action->marked_deleted($action->id);  //<---- This is so the relationships get deleted AND the logic hooks for the $action bean are triggered
	$action->Save ();
}

Thanks mikesolomon and mminnie.
I will give it a try and post here the results.

Can’t get this to work.
I’m no expert in this and maybe you can help me.

I have two custom modules:

  • DLVRY_deployed_services
  • DLVRY_service_items
    with a one-to-many relationship from DLVRY_deployed_services to DLVRY_service_items

I created two files:
custom/modules/DLVRY_deployed_services/logic_hooks.php:

<?php $hook_version = 1; $hook_array = Array(); $hook_array['before_delete'] = Array(); $hook_array['before_delete'][] = Array( //Processing index. For sorting the array. 1, //Label. A string value to identify the hook. 'before_delete example', //The PHP file where your class is located. 'custom/modules/dlvry_deployed_services/logic_hooks_class.php', //The class the method is in. 'logic_hooks_class', //The method to call. 'before_delete_method' ); ?>

and custom/modules/DLVRY_deployed_services/logic_hooks_class.php:

<?php if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point'); class logic_hooks_class { function before_delete_method($bean, $event, $arguments) { $actions = $bean->get_linked_beans ( 'DLVRY_service_items'); foreach ( $actions as $action ) { $action->marked_deleted($action->id); //<---- This is so the relationships get deleted AND the logic hooks for the $action bean are triggered $action->Save (); } } } ?>

But, when I delete a deployed_service it doesn’t delete the related service items.

I surely am doing something wrong but can’t figure what

Your logic_hooks.php file doesn’t look correct. You need to define a key for the $hook_array sub-array. Also…be careful of the case of your directories if your are on a case-sensitive OS.

It should be:


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

$hook_version = 1; 
$hook_array = Array(); 
$hook_array['before_delete'] = Array(); 
$hook_array['before_delete'][] = Array(1, 'Some description of the hook here', 'custom/modules/DLVRY_deployed_services/logic_hooks_class.php','logic_hooks_class', 'before_delete_method'); 


I was doing it wrong, I suppose.

$bean->get_linked_beans ( ‘DLVRY_service_items’); DLVRY_service_items is the bean_name of the field dlvry_deployed_services_dlvry_service_items
which is created when I create the module relationship.

However, changing it to get_linked_beans(‘dlvry_deployed_services_dlvry_service_items’); gives me a blank page when I delete the record and the apache log is this: “Call to undefined method DLVRY_service_items::marked_deleted()…”

Can’t figure it out

Any idea ?

I had a code typo above. The method call should be:

mark_deleted and not marked_deleted.

I would also suggest a good IDE and debugging environment if you don’t already use one. I use Netbeans with Xdebug and I am very satisfied.

1 Like

Thanks!

That was the problem (and I didn’t catch it)

I’m no coding guru, just trying to get something done.
Last time I really coded was C Standard :slight_smile:

Thanks on the advice!

This doesn’t work for custom modules but available from the SugarOutfitters store

https://store.suitecrm.com/addons/deleteplus