0

I need to change opportunity stageName depending on account status, if it's active or inactive. If it's inactive stageName needs to be changed to 'Closed Lost'. Otherwise, if the stageName is 'Closed Won' it doesn't need to change. We've been tying with this code but it doesn't work.

trigger OpportunityUpdate on Account (before insert,before update) {
    for(Account a : Trigger.new){
        if(a.Active__c == 'No'){
            Opportunity o = new Opportunity();
            if(o.StageName != 'Closed Won'){
                o.StageName = 'Closed Lost';
            }
        }
    }
}
1
  • 1
    Are you trying to insert new opportunity or want to update old Opp. Nov 24, 2015 at 12:29

1 Answer 1

0

There is no need for writing trigger on before insert. use only update event

trigger OpportunityUpdate on Account ( after update) {
    set<Id> setAccountId = new set<Id>();
    for(Account a : Trigger.new){
        if(a.Active__c == 'No'){
            setAccountId.add(a.Id);
        }
    }
    List<Opportunity> lstOpportunity = [SELECT Id, StageName FROM Opportunity WHERE AccountId IN: setAccountId];
    for(Opportunity objOpp: lstOpportunity)
    {
        if(objOpp.StageName != 'Closed Won'){
                objOpp.StageName = 'Closed Lost';
        }
    }
    update lstOpportunity;
}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .