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
sunny sengar 17sunny sengar 17 

Update Opportunity Stage related to associate Account.

Hi, 
I want trigger for Update Opportunity Stage associated with account, If 1 opportunity stage is closed won than other opportunity stage should be 'closed lost'. 
Best Answer chosen by sunny sengar 17
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sunny,

If account have 3 opportuinities, if any one of the opportunity closed won then rest of the opportunies should be auto updated to closed Lost.

If my understanding is correct then try with below code.
trigger updateopportunitystage on Opportunity (after UPDATE) {
Set <Id> accountIds = new Set <Id>();
List <Opportunity> lstOpportunitysToUpdate = new List <Opportunity>();
 if(Trigger.isUpdate){
    for(Opportunity Opp:trigger.new){
	if(opp.StageName =='Closed Won'){
        accountIds.add(Opp.accountID);
		}
    }
}

for(opportunity opp:[SELECT Id,StageName from Opportunity where AccountId IN: accountIds AND StageName !='Closed Won']){

    opp.StageName = 'Closed Lost';
    lstOpportunitysToUpdate.add(opp);
}

UPDATE lstOpportunitysToUpdate;
}
If this helps, Please mark it as best answer.

Thanks!!