You need to sign in to do that
Don't have an account?
Pankaj Yadav 29
how to prevent delete of account related to opportunity when opportunity
trigger OpportunityOpen on Account(before delete) { Set<Id> setacc = new Set<Id>(); for(Account a : trigger.old) { setacc.add(a.Id); } List<Account> acclist = [Select Id, Name, (Select AccountId, Name, StageName From Opportunities)From Account Where Id In :setacc]; for(Account a : acclist) { for(Opportunity opp : acclist.Opportunities) { if(opp.StageName=='closedwon') { a.addError('you should not delete the account because a closedwon opportunity'); } } } }
Please use the below Code :-
trigger OpportunityOpen on Account(before delete) {
Set<Id> setacc = new Set<Id>();
for(Account a : trigger.old) {
setacc.add(a.Id);
}
List<Account> acclist = [Select Id, Name, (Select AccountId, Name, StageName From Opportunities)From Account Where Id In :setacc]; for(Account a : acclist) {
for(Opportunity opp : a.Opportunities) {
if(opp.StageName=='Closed Won') {
a.addError('you should not delete the account because a closedwon opportunity');
}
}
}
}
if this solution is helpful for you. then Mark as Best Answer.