AnsweredAssumed Answered

Update calculated field on one-to-many relationship

Question asked by Christian Weber on Jul 23, 2019

I have 2 custom modules A (parent) and B (children). These two are linked with a one-to-many relationship (a_b)

 

Module B holds some numbers in a field lets say B_count and I set up a calculated field (A_sum) in module A like

 

rollupSum($a_b, "B_count")

 

So the expected behaviour is that in field A_sum the sum of all B_count get's saved. That is true if I save the parent record. But if I update a B_count value it does not recalculate the parent field, the value is never updated.

 

I tried the same setup with a many-to-many relationship and it worked as expected. The parent's value got updated on a child change.

 

So why is that?

 

Update: I traced the behaviour down to SugarBean.php line 2047

if ((empty($this->$lname) && !$this->load_relationship($lname)) || !($this->$lname instanceof Link2)) {
    continue;
}

With the one-to-many relationship $this->$lname is an array with the parents name and id. So it enters the condition, continuing the loop. 
With the many-to-many relationship it is empty, forcing it to load the relationship as a Link2 instance. Resulting in not skipping the update

Outcomes