+ Start a Discussion
Sainath VenkatSainath Venkat 

need help in updating stage in parent from child

I am working one trigger which will update the stage in Opportunity based on child field.
for a given opportunity, if any of the child record contains Program_Action__c = "MATR" then I want to update the status to Closed Won, if none of the child records contains MATR and only contains "WADM" or "WAPP" then stage is Closed Lost and child records contains Program_Action__c = "ADMT" then stage should be Open
My code is working for all conditions but if any of the child record contains MATR then stage is not updating to "Closed Won".
Can anyone helps me out in this issue here.
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.Opportunity_ID__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.Program_Action__c == 'MATR' && (oppPrgmObj2.Opportunity_ID__c == oppObj.Id)){
                oppObj.StageName='Closed Won';
                olist.add(oppObj);
                won=2;
            }
            else if((oppPrgmObj2.Program_Action__c == 'WADM' ||oppPrgmObj2.Program_Action__c == 'WAPP') && (oppPrgmObj2.Opportunity_ID__c == oppObj.Id)){
                    oppObj.StageName='Closed Lost';
                    olist.add(oppObj);
                    won=2;
                }

            else if((oppPrgmObj2.Program_Action__c == 'ADMT' && (oppPrgmObj2.Opportunity_ID__c == oppObj.Id)){
                oppObj.StageName='Open';
                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());
    }
}
 }

 
Krishna.AvvaKrishna.Avva

Hi Sainath,


What are the values of oppPrgmObj2.Program_Action__c and oppPrgmObj2.Opportunity_ID__c when you print them? Did you verify they are as expected and the condition matches?