If an account is assigned to another user, surely all the account’s contacts should also be reassigned. The same goes for other child records, such as documents, opportunities etc.
This requirement came up recently in the LinkedIn SugarCRM Network, so I thought I would put together a logic hook to see how it might be done.
The following logic hook was designed for Accounts, but could be applied to any module. Also I’ve specified in the code which child modules are to be updated. You may not need all related modules to be reassigned.
There are three issues that should be considered before implementing this type of logic, as follows.
(a) You will be saving related records, and if these have associated workflow or logic hooks, then these will get fired from this logic hook. You need to look carefully at each child module you use, just to be sure your are not going to initiate unwanted alters and actions.
(b) In some cases an account will have a huge number of records associated with it, and re-assigning them all via this logic hook could take some time. If this is the case it would be better to adopt a different solution (see below).
(c) When a user creates a new contact, this logic hook will not ensure that the assigned user matches the account. This is because in this case the accounts logic hook is never fired. To ensure all assignments are correct, you could also add logic hooks on each child module, or use a separate over-night process to ensure all records are assigned correctly (see below).
An alternative would be to run an overnight process to do the reassignments. This would involve similar logic, but run at a time of day that wouldn’t affect users. It would only look at recently updated records. This would handle (b) and (c) above, but not (a).
You will need to add the following to custom/modules/Accounts/logic_hooks.php
'Reassign all child records',
'custom/modules/Accounts/Reassign.php', // file where hook found
'Reassign', // class name
'doReassign'); // method to call
And add the following as a file custom/modules/Accounts/Reassign.php
* Class to reassign all account child records, to match
* the account just saved
* NOTE make sure that saving child records won't trigger any workflow or
* logic hooks
// all the related modules we want to update
private $childModules = array(
public function doReassign($bean, $event, $arguments)
// keep the assigned user for this bean
$assigned_user_id = $bean->assigned_user_id;
// ok, we need to change assigned user on all child records
// get the bean to load all its relationships
$links = $bean->get_linked_fields();
foreach($links as $relName => $rel)
// ignore relationships we dont want
if(!in_array($relName, $this->childModules)) continue;
// lets load this related record
$ok = $bean->load_relationship($relName);
if($ok == false) trigger_error("Cant load rel $relName", E_USER_ERROR);
// get all the records for this relationship
$relBeans = $bean->$relName->getBeans();
foreach($relBeans as $relBean)
// if the rel bean is assigned to the correct user, dont bother saving
if($assigned_user_id == $relBean->assigned_user_id) continue;
// set this record to match parents assigned user
$relBean->assigned_user_id = $assigned_user_id;
// save without notifying anyone
I hope this is helpful. If you can suggest any improvements or have any comments, they would be very much appreciated.