AnsweredAssumed Answered

Notes, Meetings and Tasks under an Opportunity are set to Account when changing assigned user in a before save logic hook

Question asked by Jeroen Somhorst on May 23, 2014
Latest reply on Jun 3, 2014 by Jeroen Somhorst
Currently I'm constructing a before save logic hook on Accounts that does the following:

When the user changes the assigned user for an Account the assigned user will also be changed to all related records from all related modules (off course with bookkeeping to prevent e.g. system modules changes etc.).

This works without problems except for one thing:
Suppose Account A has Opportunity B with Task C. So when you would open Task C then it would show that it is related to Opportunity B. When I would assign another user to Account A then suddenly Task C is not related to Opportunity B anymore, but directly to Account A.
Also, when I would do a mass update then the relation from Task C to Opportunity B would be untouched; this only occurs when a single Account is altered.

I have the following code which processes these assigment changes (left out bookkeeping for readability) :

$linked_fields = $bean -> get_linked_fields();

foreach ($linked_fields as $name => $properties) {
$bean -> load_relationship($name);

$relatedBeans = $bean -> $name->getBeans();
foreach ($relatedBeans as $relatedBean) {
  $relatedBean -> assigned_user_id = $newAssigned;
  $relatedBean -> save();
  $this -> recursiveAssignedChange($relatedBean); 

So this is a recursive function which starts from the Account bean and loads for all relationships the related beans, and makes a recursive call with each related bean.

So I'm wondering how this behavior is caused, and whether it can be prevented in a way.
Currently I have a work-around for this issue by just executing an SQL update query, but I'd rather keep working with beans because it makes the code upgrade-safe.

Thanks in advance!