+ Start a Discussion

Field calculation based on related objects



I have a custom object A which includes a reference via Lookup Relationship to a related custom object B.

I want to calculate and update the 'Grand Total' field on B which is the sum of all 'Price' fields on all related As.

How do I do it?




If it's a master-detail relationship you can use Rollup Summary Fields and it couldn't be easier. If it's a lookup field you need a trigger on your child object.


When a child object is created or changed you need to SOQL for all the children of the parent and calculate the total. If a child changes from one parent to another, you'll have to recalc both parents.




Thank you


I wish it was a Master-Detail, then it was really simple but the Master-Detail does not allow me to change the Master after saving which is something I need to do throughout the life cycle of the record.


I created a trigger based on the Shipping Invoice Example Code in the Apex Guide but it looks too complicated for a simple task.

Am I missing something?