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
Salesforce Admin 110Salesforce Admin 110 

3 opps in 1 account if close win 1 opp how do i close lose other 2 opps?

3 opps in 1 account if close win 1 opp how do i close lose other 2 opps?
@Karanraj@Karanraj
You use Process builder to accomplish this.

1. Create a process builder for the 'Opportunity Object' and select the condition as 'when a record is created or edited'
2. In the condition meets set it as 'Stage equals Closed Won'
3. In the 'Immediate action' select update records and set the criteria as 'AccountId equals Opportunity.AccountId' and set the values for Stage as 'Closed Lost'
4. Then activate your process builder 

User-added image


 
Salesforce Admin 110Salesforce Admin 110
i need to do this in apex
Bhaswanthnaga vivek vutukuriBhaswanthnaga vivek vutukuri

@Salesforce Admin - you can do it using trigger on Opportunity.

trigger colseOpportunity on Opportunity (before insert, before update)
{

  if((Trigger.isbefore && Trigger.isinsert) || (Trigger.isbefore && Trigger.isupdate))
    {

        List<Id> opportunityAccountIds = new List<Id>();
        for(Opportunity o:Trigger.new)
        {       
             if(b.Status== 'close won')
             {
                opportunityAccountIds.add(b.AccountId);
             }
        }

        
        for(Opportunity opp:[SELECT Id, Status FROM Opportunity WHERE AccountId in: opportunityAccountIds AND AccountId != Null])
        {
            opp.Status = 'closed won';
        }
        
    
    }


}
 

 

Himanshu MaheshwariHimanshu Maheshwari
Try the below code
 
trigger UpdateStageOpportunity on Opportunity (before insert, before update){
    Set<Id> accountIdSet = new Set<Id>();
    List<Opportunity> oppList = new List<Opportunity>();
    for(Opportunity opp : Trigger.new) {       
         if(Trigger.isInsert && opp.StageName == 'Closed Won') {
            accountIdSet.add(opp.AccountId);
         }

         if(Trigger.isUpdate && opp.StageName == 'Closed Won' && opp.StageName != Trigger.oldMap.get(opp.Id).StageName){
            accountIdSet.add(opp.AccountId);
         }
    }

    for(Opportunity opp:[SELECT Id, StageName FROM Opportunity WHERE AccountId IN: accountIdSet AND Id NOT IN: Trigger.New]){
        opp.Status = 'closed won';
        oppList.add(opp);
    }

    if(oppList.size() > 0){
        update oppList;
    }
}

Thanks,
Himanshu