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
soMuchToLearnsoMuchToLearn 

Updating Children Records: Trying to Learn Triggers

This is my first trigger and I only have an admin background so I’m stuck early on. I have Master Opportunities that have children opportunities off of them. If the master opportunity is marked Closed Lost then the Children all need to be updated to a stage of Closed Lost as well. I know in English what I want to do:

 

I Need an "After Update" trigger.  
I Need to take the id of the master Opportunity and use it to query for a list of child Opportunities.

I Need to Loop thru the children, to see if the Stage Value of the child is not “Closed Lost”.
If it is not “Closed Lost” I need to add each child to a list for updating.
Finally I need to update the Stage on the list of Children once I’ve checked all of them. 

 

I have a start...

trigger updateChildOpportunity on Opportunity (After update) {

and now I’m stuck :-(

Andrew WilkinsonAndrew Wilkinson

I am assuming the name of your parent lookup field as parent_Opportunity__c you will need to change that...in the soql any fields you want to update on the child oppotunities will need to be queried and then changed in the for loop

 

Set<ID> oppIds = new Set<ID>();

List<Opportunity> oppsToUpdate = new List<Opportunity>();

oppIds.addall(Trigger.newMap.keySet());

 

for(Opportunity o : [SELECT Id, StageName...maybe some other fields FROM Opportunity WHERE parent_Opportunity__c IN :oppIds]){

    if(o.StageName != 'Closed Lost'){

        //do something in here with the record

        oppsToUpdate.add(o);

    }

}

 

update oppsToUpdate;

 

 

 

This should get you started.