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
case commentscase comments 

Opportunity stagename update trigger

On opportunity, I have a lookup to case.  Whenever a case is moved to status 'Cancelled/declined', I want the opportunity stage to go to "Closed-Lost".  I have written a trigger below, but it doesn't seem to be working.  Any help would be greatly appreciated.

 

trigger ClosedLostOpp2 on Case (after update) {
    //create list of case IDs
    List<ID> cid = New List<ID>();
    //find cases to add to list
    for(Case c: Trigger.new){
        if(c.Status == 'Cancelled/declined'){
            cid.add(c.Id);
        }
       
             
             
             
     List<Opportunity> OppUpdateList = [SELECT Id, StageName from Opportunity WHERE ID in:cid];
        List<Opportunity> opp = new List<Opportunity>();
        for (Opportunity oppsupd : oppUpdateList)
        {
            oppsupd.StageName = 'Closed-Lost';
            opp.add(oppsupd);
        }
        update opp;
        
            

}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

You were almost there !

You made a silly mistake.

 

trigger ClosedLostOpp2 on Case(after update) {
    //create list of case IDs
    List < ID > cid = New List < ID > ();
    //find cases to add to list
    for (Case c: Trigger.new) {
        if (c.Status == 'Cancelled/declined') {
            cid.add(c.Id);
        }

    }
    List < Opportunity > OppUpdateList = [SELECT Id, StageName from Opportunity WHERE Case__c in : cid];
   
    for (Opportunity oppsupd: oppUpdateList) {
        oppsupd.StageName = 'Closed-Lost';
    }
    update oppUpdateList;


}

 Well also the query should be moved out of the loop. In the above code the query and the update happens outside the loop

All Answers

Avidev9Avidev9

You were almost there !

You made a silly mistake.

 

trigger ClosedLostOpp2 on Case(after update) {
    //create list of case IDs
    List < ID > cid = New List < ID > ();
    //find cases to add to list
    for (Case c: Trigger.new) {
        if (c.Status == 'Cancelled/declined') {
            cid.add(c.Id);
        }

    }
    List < Opportunity > OppUpdateList = [SELECT Id, StageName from Opportunity WHERE Case__c in : cid];
   
    for (Opportunity oppsupd: oppUpdateList) {
        oppsupd.StageName = 'Closed-Lost';
    }
    update oppUpdateList;


}

 Well also the query should be moved out of the loop. In the above code the query and the update happens outside the loop

This was selected as the best answer
Daniel Zeidler (DZ)Daniel Zeidler (DZ)

I think the problem is with this line

 

List<Opportunity> OppUpdateList = [SELECT Id, StageName from Opportunity WHERE ID in:cid];

It is querying all the opps whose Id is in the "cid" list. But that is a list of Id's for cases, not opportunities. Your code should look more like

 

List<Opportunity> OppUpdateList = [SELECT Id, StageName from Opportunity WHERE case__c in:cid];

where you replace case__c with the api name of your look up field on the opportunity.

Marko LamotMarko Lamot

additionaly you have too many parenthis (problem is after first FOR) .... which will cause dml error on mass update

 

 

trigger ClosedLostOpp2 on Case (after update) {

List<ID> cid = New List<ID>();
for(Case c: Trigger.new) if(c.Status == 'Cancelled/declined') cid.add(c.Id); List<Opportunity> OppUpdateList = [SELECT Id, StageName from Opportunity WHERE case__c in:cid];

for (Opportunity o:OppUpdateList)
o.StageName = 'Closed-Lost';

if(
OppUpdateList!=null) update OppUpdateList;

}
case commentscase comments

I was so close....thanks for the quick replies!