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
Sainath VenkatSainath Venkat 

trigger to update parent from child record status

I need help in writing a trigger here. I have two objects

1) Opportunity
2) Opportunity_Programs__c

I have lookup relationship between them, opportunity is parent and Opportunity_Programs__c is child, so one opportunity can have multiple Opportunity_Programs__c.

I have Status__c field in Opportunity_Programs__c object, so if Opportunity_Programs__c records that related to particular opportunity has Status__c value as "Open" then I need to update Opportunity stage to Open, if Stage__c is "Close" then I need to update Opportuntiy stage to "Closed Lost"

Can anyone help me out in this issue here
Khan AnasKhan Anas (Salesforce Developers) 
Hi Sainath,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger UpdateOpp on Opportunity_Programs__c (after insert, after update) {

    List<Id> oppIds = new List<Id>();
    List<Opportunity> opportunites = new List<Opportunity>();
    
    for(Opportunity_Programs__c o : trigger.new){
        oppIds.add(o.Opportunity__c); // API name of parent in child
    }
    
    for(Opportunity o : [SELECT Id, StageName FROM Opportunity WHERE Id IN :oppIds]){
        for(Opportunity_Programs__c opp : trigger.new){
            if(opp.Status =='Open'){
                o.StageName='Open';
                opportunites.add(o);
            }
            if(opp.Status =='Close'){
                a.StageName='Closed Lost';
                opportunites.add(o);
            }
        }
    }
    if(opportunites.size()>0){
        UPDATE opportunites;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Sainath,

Use the blow code it will helpfull for you.

-----trigger----------
trigger UpdateParentFromChild on Opportunity_Programs__c (After Insert , After update) {
if(trigger.IsAfter && (trigger.IsInsert || trigger.Isupdate))
    {
       ParentUpdateFromChildOpportunity.ParentUpdate(trigger.new);
    }
}
------apex code----------
public class ParentUpdateFromChildOpportunity {
    public static void ParentUpdate(List<Opportunity_Programs__c> oppProgramList){
        try{
                System.debug('Opportunity List==>'+oppProgramList);

        Set<Id> opportunityId = new Set<Id>();
        for(Opportunity_Programs__c oppPrgmObj : oppProgramList){
            opportunityId.add(oppPrgmObj.OppotunityID__c);
        }
        List<Opportunity> opportunityList = New List<Opportunity>();
        opportunityList = [Select Id,
                           StageName
                           From Opportunity
                           Where Id IN : opportunityId
                           Limit 50000
                          ];
        Integer won=1;
        List<Opportunity> olist=new List<Opportunity>();
        for(Opportunity_Programs__c oppPrgmObj2 : oppProgramList){
            for(Opportunity oppObj : opportunityList){
                if(oppPrgmObj2.Statuss__c == 'Open' && (oppPrgmObj2.OppotunityID__c == oppObj.Id)){
                    oppObj.StageName='Closed Won';
                    olist.add(oppObj);
                    won=2;
                }
                else{
                    if(oppPrgmObj2.Statuss__c == 'Close' && (oppPrgmObj2.OppotunityID__c == oppObj.Id)){
                        oppObj.StageName='Closed Lost';
                        olist.add(oppObj);
                        won=2;
                    }
                }
            }
        }
        if(won == 2){
            system.debug('enter won');
            Update olist;
        }
        }catch(Exception e){
            system.debug('message::'+e.getMessage()+'line::'+e.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Sainath VenkatSainath Venkat
@Ajay Dubedi,

thanks for the help but the trigger that you provide is not looping all the child records, it just looking into the inserted record but not existing child record, if all the child records are open then my trigger should set stage to Closed Won, if any of the child contains status as close then stage should be closed Lost