function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tommy CTommy C 

How to update a parent record in a trigger if the parent record is the same kind of object as the child object being triggered?

Hi all,

I have some code that I would like to ensure updates from the child Opportunities to the parent Opportunity, which is a renewal. Right now, I have a beforeUpdate trigger that looks to see if the Opportunity has any children opportunities. If it does, then it will sum up the values of Currency1__c, Currency2__c, and Currencye3__c from the child opportunities into the parent's Opp fields rollupCurrency1,rollupCurrency2 , and rollupCurrency3. I have this part written out.

The second part is a little difficult for me to figure out. Essentially, I want it so that if any of three specific Currency fields are changed on the child opportunity, it should trigger an update in the parent Opportunity. They are associated via a lookup field. Since it's the same object, I'm not sure what the best practice would be to update the parent Opportunity when the Opp Trigger is triggered from the child Opportunity since it's not passed in Trigger.New, and DML operations don't work on the same object if I recall correctly. Any help is appreciated! 

 
Tommaso BolisTommaso Bolis
you can define a trigger on the child opportunity (I would say in after insert/update).
Inside the handler you need to retrieve the parent opportunity with a soql and then update it.