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
dpardpar 

write a trigger for when opportunity stage change to closed won the custom checkbox on account object becomes true

Khan AnasKhan Anas (Salesforce Developers) 
Hi Digna,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger UpdateAccOnchangeOpp on opportunity (after insert, after update){
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Opportunity o : trigger.new){
        accIds.add(o.accountId);
    }
    
    for(Account a : [SELECT Id, Checkbox__c FROM Account WHERE Id IN :accIds]){
        for(Opportunity opp:  trigger.new){
            if(opp.StageName == 'Closed Won'){
                a.Checkbox__c=true;
                accounts.add(a);
            }
        }
    }
    if(accounts.size()>0){
         Update accounts;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Jolly_BirdiJolly_Birdi
trigger UpdateAccOnchangeOpp on opportunity (after insert, after update){
    
    Set<Id> accIds = new Set<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Opportunity Opp : trigger.new){
        if(Opp.StageName == 'Closed Won'){
               accIds.add(Opp.accountId);
        }
    }
    if(!accIds.isEmpty()){
         for(Id acc : accIds){
                accounts.add(new Account(Id=acc,Checkbox__C=true));
         }
         Update accounts;
    }
}
Hello @Digna

Please check this Optimised Code for the your query and Please mark this as best answer if you find it positive.

Thanks
Jolly Birdi
 
dpardpar
Hello, Khan Anas  
I have already solved it
Thanks,
Digna Parmar
Raj VakatiRaj Vakati
try this
 
trigger UpdateAccOnchangeOpp on opportunity (after insert, after update){
    
    List<Id> accIds = new List<Id>();
    List<Account> accounts = new List<Account>();
    
    for(Opportunity o : trigger.new){
		 if(o.StageName == 'Closed Won'){
          accIds.add(o.accountId);
		 }
    }
    
    for(Account a : [SELECT Id, Checkbox__c FROM Account WHERE Id IN :accIds])
	{
                a.Checkbox__c=true;
                accounts.add(a);
	}
        
    
    if(accounts.size()>0){
         Update accounts;
    }
}

 
dpardpar
I want bulk safe . Is it possible and how?
Ajay K DubediAjay K Dubedi
Hi Digna,

Below Code can fulfill your requirements. Hope this will work for you.
trigger UpdateAccOnchangeOpp on opportunity (after insert, after update){

    List<Account> newaccountsList = new List<Account>();
    Set<Id> accIds = new Set<Id>();
    List<Account> accountsList = new List<Account>();
    accountsList = [SELECT Id, Checkbox__c FROM Account WHERE Id IN :accIds];
    
    for(Opportunity opp : trigger.new){
         if(opp.StageName == 'Closed Won'){
          accIds.add(opp.accountId);
         }
    }
    
    for(Account acc : accountsList)
    {
       acc.Checkbox__c=true;
       newaccountsList.add(acc);
    }
        
    
    if(newaccountsList.size()>0){
         Update newaccountsList;
    }
}

Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi
dpardpar
@Ajay K Dubedi
I want the result using Map 
suresh gupta 12suresh gupta 12
trigger ActiveAccIfAllOppsAreClosed on Opportunity (After insert, After update, After Delete) {
     list<Id> SetAccountId = new list<Id>();
    list<Account> acclist = new list<Account>();
   // Map<id,List<opportunity>> mapAccIdAndOpp = new Map<id,List<opportunity>>();
    list<opportunity>oppList = new list<opportunity>();
    If(Trigger.isInsert){
        For (Opportunity opp:Trigger.new){
            SetAccountId.add(opp.AccountId);
        }
    }
        If(Trigger.isUpdate || Trigger.IsDelete){
        For (Opportunity opp:Trigger.old){
            SetAccountId.add(opp.AccountId);
        }
    }
    list<Account> a = [Select id, Active__c, (SELECT Id, StageName FROM Opportunities) FROM Account WHERE id IN :SetAccountId];
    for (Account  ac:a){
        for(Opportunity op : ac.Opportunities){
            if (op.StageName =='Closed Won'){
                ac.Active__c = True;
            }else{
                ac.Active__c = False;
            }
            if(!acclist.contains(ac)){
                acclist.add(ac);
            }
        }
    }
     System.debug('result  '+acclist);
     Update acclist;
}



Worked in my case