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 opportunity stage from child object

I need help in writing a trigger, I have two objects

1) Opportunity
2) Opportunity_ Program__c

both are having lookup relationship, Opportunity is Parent and Opportunity Program is child.

Opportunity program has Status field.

I need to write a trigger on Opportunity Program object, for a given Opportunity if any of the child records contains status as "MATR" then Opportunity.StageName = 'Closed Won', if any of the child record for a given opportunity does not contain "MATR" then Opportunity.StageName = 'Closed Lost'

Can anyone helps me out in this issue please.
StephenDicksonStephenDickson
Hi Sainath,

Would it work with your business process if the trigger on Opportunity Program, on update, checks to see if the Status contains 'MATR' and if so simply changes the status of the Opportunity to 'Closed Won'?  And then vice versa, if an Opportunity Program Status is changed to not contain 'MATR', then the Status of the Opportunity is changed to 'Closed Lost' (if not already 'Closed Lost').  

Do you have more than one picklist value on the Status field that contains 'MATR'?  Or would the value that you are checking against simply be 'MATR'?
Soyab HussainSoyab Hussain
Hi Sainath Venkat,

You can try this code this will help you.

You need to change the field API names according to your fields.
 
trigger OpportunityProgramTrigger on Opportunity_Program__c (after insert) {
    Map<String, String> uniqulyIdentify = new Map<String, String>();
    List<Opportunity__c> updateOppList = new List<Opportunity__c>();
    for(Opportunity_Program__c oppProgram : Trigger.New) {
        if(oppProgram.YourStatusField == 'MATR' && oppProgram.Opportunity__c != null && !uniqulyIdentify.containsKey(oppProgram.Opportunity__c)) {
            uniqulyIdentify.put(oppProgram.Opportunity__c, '');
            updateOppList.add(new Opportunity__c(Id = oppProgram.Opportunity__c, StageName = 'Closed Won'));
        }else if(oppProgram.YourStatusField != 'MATR' && oppProgram.Opportunity__c != null && !uniqulyIdentify.containsKey(oppProgram.Opportunity__c)) {
            uniqulyIdentify.put(oppProgram.Opportunity__c, '');
            updateOppList.add(new Opportunity__c(Id = oppProgram.Opportunity__c, StageName = 'Closed Lost'));
        }
    } 
    if( !updateOppList.isEmpty() ) {
        update updateOppList;
    }
}

If you found it useful please appreciate my efforts and mark it as the best answer

Regards,
Soyab
Sainath VenkatSainath Venkat
@StephenDickson,Soyab Hussain

Thanka a lot for helping me out here

If any of the status of Opportunity program contains 'MATR' then stageName= closed won
If none of the status of Opportunity Program contains 'MATR' but contains either 'WADM' or 'WAMM' then stage should be closed lost
else if opportunity program for a particular opportunity contains 'DENY' or 'LITE' THEN STAGE SHOULD BE OPEN
StephenDicksonStephenDickson
This is not a bulkified solution, but this would be my first idea with this trigger:
 
trigger OpportunityProgramTrigger on Opportunity_Program__c (after insert) {

    for (Opportunity_Program__c oppProg : Trigger.new) {
        // Get parent Opportunity 
        List<Opportunity> opp = [SELECT Id,
                                 	    StageName
                                   FROM Opportunity
                                  WHERE opp.Id = oppProg.Opportunity__c];
        
        // Get list of all Opportunity_Program__c child records for the Opportunity
        List<Opportunity_Progam__c> relatedOppProgs = [SELECT Id,
                                                          	  Status__c
                                                         FROM Opportunity_Program__c
                                                        WHERE Opporunity__c = :opp.get(0).Id];
        // If Statements
            for = (Opportunity_Program__c relatedOppProg : relationOppProgs) {
                if (relatedOppProg.Status__c.contains('MATR')) {
                    opp.get(0).StageName = 'Closed Won';
   
                } else if (!relatedOppProg.Status__c.contains('MATR') 
                           AND (relatedOppProg.Status__c.contains('WADM')
                               OR relatedOppProg.Status__c.contains('WAMM'))) {
                                   opp.get(0).StageName = 'Closed Lost';
                                   
                } else if (relatedOppProg.Status__c.contains('DENY') 
                           OR relatedOppProg.Status__c.contains('LITE')) {
                               opp.get(0).StageName = 'OPEN';
                           }
            }
        update opp;
    }
}

 
Soyab HussainSoyab Hussain
Hi Sainath Venkat,

Use this code maybe this will help you.
 
trigger OpportunityProgramTrigger on Opportunity_Program__c (after insert) {
    Map<String, String> uniqulyIdentify = new Map<String, String>();
    List<Opportunity__c> updateOppList = new List<Opportunity__c>();
    for(Opportunity_Program__c oppProgram : Trigger.New) {
        if(oppProgram.YourStatusField != null && oppProgram.YourStatusField == 'MATR' && oppProgram.Opportunity__c != null && !uniqulyIdentify.containsKey(oppProgram.Opportunity__c)) {
            uniqulyIdentify.put(oppProgram.Opportunity__c, '');
            updateOppList.add(new Opportunity__c(Id = oppProgram.Opportunity__c, StageName = 'Closed Won'));
        }else if(oppProgram.YourStatusField != null && (oppProgram.YourStatusField == 'WADM' || oppProgram.YourStatusField == 'WAMM') && oppProgram.Opportunity__c != null && !uniqulyIdentify.containsKey(oppProgram.Opportunity__c)) {
            uniqulyIdentify.put(oppProgram.Opportunity__c, '');
            updateOppList.add(new Opportunity__c(Id = oppProgram.Opportunity__c, StageName = 'Closed Lost'));
        }else if(oppProgram.YourStatusField != null && (oppProgram.YourStatusField == 'DENY' || oppProgram.YourStatusField == 'LITE') && oppProgram.Opportunity__c != null && !uniqulyIdentify.containsKey(oppProgram.Opportunity__c)) {
            uniqulyIdentify.put(oppProgram.Opportunity__c, '');
            updateOppList.add(new Opportunity__c(Id = oppProgram.Opportunity__c, StageName = '')); // enter open stage value 
        }
    } 
    if( !updateOppList.isEmpty() ) {
        update updateOppList;
    }
}
Regards,
Soyab
Ajay K DubediAjay K Dubedi
Hi Sainath,

I have understood you requirment and tried to find out the solution. I believe it will help you.
I have given below solution for the conditions-
1. Opportunity_ Program__c has a lookup for Opportunity.
2. Opportunity_ Program__c record has a status field.
 
trigger updateOpportunityStatus on Opportunity_ Program__c(after insert , after update)
  {
   Opportunity_ Program__c opc = [select opportunity__c from Opportunity_ Program__c where id in      :trigger.new];
   //replace opportunity__c by your opportunity lookup Api name.
   
   Id parrentOppId = opc.opportunity__c;

List<Opportunity_ Program__c> opcList = [select id ,status__c from Opportunity_ Program__c where     opportunity__c =: parrentOppId AND status__c ='MATR'];

Opportunity opp = [select stageName from opportunity where id =:parrentOppId];

if(opcList.size()>1)
{
  opp.stageName = 'Closed Won';
}

else
opp.stageName = 'Closed Lost';


update opp;

  }

That's all if you have more queries please let me know.


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
Deepali KulshresthaDeepali Kulshrestha
Hi Sainath,

I have gone through made the same object and trigger it is working correctly.

Trigger to update the opportunity with is child item is given below:-
Code ==>

trigger TriggerToUpdateOppStage on Opportunity_Program__c (before insert,before update) {
    if((trigger.isInsert || trigger.isUpdate)&& trigger.isBefore ){
        Set<Id>  oppID = new Set<ID>();
        for(Opportunity_Program__c op : trigger.new){
            if(op.Opportunity__c != null){
                oppID.add(op.Opportunity__c);
            }
        }
        List<Opportunity>  opplist = [Select id,Name,StageName from Opportunity where id in: oppID];
        List<Opportunity> updtOpp = new List<Opportunity>();
        for(Opportunity_Program__c op : trigger.new){
            if(oppID.contains(op.Opportunity__c) && op.Status__c == 'MATR'){
                for(Opportunity o : opplist){
                    o.stageName = 'Closed Won';
                    updtOpp.add(o);
                }
            }
            else{
                for(Opportunity o : opplist){
                    o.stageName = 'Closed Lost';
                    updtOpp.add(o);
                }
            }
        }
        update updtOpp;
    }
}


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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
 
Altamash JavierAltamash Javier
Download Commando 3 Full Movie From Here Commando 3 full movie (https://mkvflex.com/commando-3-movie-download-leaked-by-tamilrockers/)