You need to sign in to do that
Don't have an account?
case 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; } }
You were almost there !
You made a silly mistake.
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
You were almost there !
You made a silly mistake.
Well also the query should be moved out of the loop. In the above code the query and the update happens outside the loop
I think the problem is with this line
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
where you replace case__c with the api name of your look up field on the opportunity.
additionaly you have too many parenthis (problem is after first FOR) .... which will cause dml error on mass update
I was so close....thanks for the quick replies!