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 then i have to check all related Opportunity will be Closed Lost then Account CheckBox will be true other wise trigger will not be fire
please guide me how to do
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;
}
}
above is my code but didn't work in above scenario if account has more than one opportunity and stage of opportunity is not closed then account checkbox becomes true that should not be true
so please guide where i change my code