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
Vikram Singh 157Vikram Singh 157 

Hi ! My requirement is :- Prevent user to delete any account with opportunity

trigger preventDelete on account(before delete){
For(account a : trigger.old){

   If(a.Opportunities != Null)
      a.addError('You cannot delete this');
}
}
But it will also not allowing to delete account with no opportunity.
 
Best Answer chosen by Vikram Singh 157
Nayana KNayana K
You can do this in few ways:

Approach1:
trigger preventDelete on account(before delete)
{
	for(Account objAcc : [SELECT Id, (SELECT Id FROM Opportunities) FROM Account WHERE Id IN : trigger.oldMap.keySet()])  
	{
		// if children opportunities present
		if(!objAcc.Opportunities.isEmpty())
		{
			trigger.oldMap.get(objAcc.Id).addError('You cannot delete this');
		}
	}
}


Approach2:
trigger preventDelete on account(before delete)
{

	Id idAcc;
	// if there are children opportunities to an account, then control goes inside the for-loop.
	for(AggregateResult ar : [SELECT AccountId, COUNT(Id)
							  FROM Opportunity 
							  WHERE AccountId IN : trigger.oldMap.keySet()
							  GROUP BY AccountId])  
    {
		trigger.oldMap.get((Id)ar.get('AccountId')).addError('You cannot delete this');
	}
}

 

All Answers

Devanshu soodDevanshu sood
trigger preventDelete on account(before delete){
    list<account>acc=new list<account>();
    list<account>acc2=new list<account>();

    For(account a : trigger.new){
        acc.add(a);
        
    }
    list<opportunity> opp=new list<opportunity>([select id from opportunity where accountId IN :acc]);
    
    for(account a:acc){
        
     if(opp.size()==0){
            delete a;
        }
        else{
            a.addError('You cannot delete this');
        }
    }
    
}

try this
Nayana KNayana K
You can do this in few ways:

Approach1:
trigger preventDelete on account(before delete)
{
	for(Account objAcc : [SELECT Id, (SELECT Id FROM Opportunities) FROM Account WHERE Id IN : trigger.oldMap.keySet()])  
	{
		// if children opportunities present
		if(!objAcc.Opportunities.isEmpty())
		{
			trigger.oldMap.get(objAcc.Id).addError('You cannot delete this');
		}
	}
}


Approach2:
trigger preventDelete on account(before delete)
{

	Id idAcc;
	// if there are children opportunities to an account, then control goes inside the for-loop.
	for(AggregateResult ar : [SELECT AccountId, COUNT(Id)
							  FROM Opportunity 
							  WHERE AccountId IN : trigger.oldMap.keySet()
							  GROUP BY AccountId])  
    {
		trigger.oldMap.get((Id)ar.get('AccountId')).addError('You cannot delete this');
	}
}

 
This was selected as the best answer
Devanshu soodDevanshu sood
trigger preventdelete on Account (before delete)  
{  
    List<Account> accList = new List<Account>();  
    Set<id> accIdSet = new Set<id>();  
    for(Account acc : Trigger.old)  
    {  
        accIdSet.add(acc.id);  
    }  

    Map<Id, Account> accts = new Map<Id, Account>([Select Id, (Select Id from opportunities) from Account where id in :accIdSet]);

    for(Account acc : Trigger.old)
    {
        if(accts.get(acc.id).opportunities.size()>0)
        {
            acc.adderror('Account cannot be deleted');
            }
        }                                       

}
it is working 100%
try this
 
Vikram Singh 157Vikram Singh 157
Thnaks Nayana K  & 
devanshu sood for your help .