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
    Are you trying to insert new opportunity or want to update old Opp. – Tushar Sharma Nov 24 '15 at 12:29
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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.