You need to sign in to do that
Don't have an account?

Account Related Opportunities Trigger . how to used only one single loop not a nested loop
trigger OpportunityOpen on Account(before delete)
{
Map<Id, Account> mapacc = new Map<Id, Account>( [Select Id, Name, AnnualRevenue,
(Select AccountId, StageName From Opportunities)
From Account Where Id In : trigger.old]);
for(Account a : trigger.old)
{
for(Opportunity opp : mapacc.get(a.Id).Opportunities)
{
if(opp.stageName=='closed won')
{
a.addError('associated open opportunities with account');
}
}
}
}
{
Map<Id, Account> mapacc = new Map<Id, Account>( [Select Id, Name, AnnualRevenue,
(Select AccountId, StageName From Opportunities)
From Account Where Id In : trigger.old]);
for(Account a : trigger.old)
{
for(Opportunity opp : mapacc.get(a.Id).Opportunities)
{
if(opp.stageName=='closed won')
{
a.addError('associated open opportunities with account');
}
}
}
}
Can you please try below code.