+ Start a Discussion
Bablu Kumar PanditBablu Kumar Pandit 

Trigger to Find Account Realed all Opportunity will Closed Lost.

Hii Every One,

i have a custom filed on Account Name(AssignCheckBox) ,write a Trigger on Opportunity when i update Stage of Opportunity is closed Lost and AccountId not Null and if Selected Account (Might be possible selected Account have more than one Opportunity) of all opportunity will be Closed Lost then Account CheckBox will be true
ShirishaShirisha (Salesforce Developers) 
Hi Bablu,

Greetings!

Please find the sample code below:
trigger Opportunitystageclosedwon_updatesaccount on Opportunity (after insert,After update) {
    list<account> accounts =  new list<account>();
    for(account acc : [select name, AssignCheckbox__c from account]){
    for(opportunity opp : trigger.new){
        if(opp.stagename == 'closed won'){
            acc.AssignCheckbox = true;
            accounts.add(acc);
        }
    }
  }update accounts;
}

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

Warm Regards,
Shirisha Pathuri
Bablu Kumar PanditBablu Kumar Pandit
Hi Shirisha,

Code didn't work according to my Requirement 

My Requirement is when i update Opportunity Closed Lost and If All Realed Opportunity of Account should be Closed Lost then i update Account Filed checkBox Shuold be true

Below see my Code but didn't work
public class OppPrintCampaign{
    public static void InsertOpp(List<Opportunity> lstopp ,Map<Id,Opportunity> mapIdToSatge){
        Map<Id,Id> mapAccIdToOppId = new Map<Id,Id>();
        Set<Id> setOfAccId = New Set<Id>();
        Set<Id> setOfOppId = New Set<Id>();
        List<Account> lstacc = New List<Account>();
        Boolean check = false;
        //Campaign objcamp = [Select Id,name from campaign where Id = '7012v000002KBp9'];
        for(Opportunity objopp:lstopp){
            if(objopp.accountId !=null && objopp.stageName == 'Closed Lost' && objopp.stageName != mapIdToSatge.get(objopp.Id).stageName){
                setOfAccId.add(objopp.accountId);
            }
		}
        System.debug('-----setOfAccId-----'+setOfAccId);
        System.debug('----setOfOppId----'+setOfOppId);
         for(Opportunity objopp :[Select Id,Name ,Stagename ,AccountId from Opportunity where AccountId IN:setOfAccId]){        
             if(objopp.StageName == 'Closed Lost'){
                 check = true;
             }
        }
        System.debug('----check----'+check);
         for(Account objacc:[Select Id,Name from Account where Id IN:setOfAccId]){
             if(check == true){
             objacc.Assign_Engments__c = true;
             lstacc.add(objacc);
             }
        }
        System.debug('-----lstacc-----'+lstacc);
        update lstacc;
	}
}

 
ShirishaShirisha (Salesforce Developers) 
Have you tried to capture the debug logs and did you see any kind of error while executing the above code.So that,we can try to help you with the error to proceed.

Thank you!