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
Avinash AngasayAvinash Angasay 

Dml Question

Delete those Accounts who have more than 2 opportunities

If anyone Can write query for it.....
I got problem  While deleting Account...\
I have written code like below---
but when i  want to delete account at the end then it will not get deleted it is saying that account have cases...what to do now...how to process it forward from here in code for deleting these account records who has opportunities grater than 2 with them.....

  List<Account> aList = [Select Name,(Select Name from Opportunities) from Account];
         List<Account> delAcc = new List<Account>();
         for(Account a:alist)
         {
           if(a.Opportunities.size()>2)
           {
               system.debug(a.Opportunities.size());
               delacc.add(a);
           }
         }
         system.debug(delacc);
         delete delacc;
    } 
Best Answer chosen by Avinash Angasay
Shiraz HodaShiraz Hoda

Hi Avinash,

Please use below code. Kindly mark as best answer if this resolves your issue.

List<Account> aList = [Select Name,(Select Name from Opportunities) from Account];
set<Id> setAccIds = new set<Id>();
List<case> caseListDel = new List<Case>();
         List<Account> delAcc = new List<Account>();
         for(Account a:alist)
         {
           if(a.Opportunities.size()>2)
           {
               system.debug(a.Opportunities.size());
			   setAccIds.add(a.Id);
               delacc.add(a);
           }
         }
		 for(case c: [select id from case where accountId IN: setAccIds]){
		 caseListDel.add(c);
		 }
		 
		 if(!caseListDel.isEmpty()){
		 delete caseListDel;
		 }
         if(!delacc.isEmpty()){
         delete delacc;
		 }

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Avinash,

Can you confirm if there are any cases for the account do you want to delete them as well?

Thanks
 
Avinash AngasayAvinash Angasay
yes also want to delete them
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Avinash,

Can you try by using below logic where we delete the cases first and then delete the Accounts.
 
List<Account> aList = [Select Name,(Select Name from Opportunities) from Account];
         List<Account> delAcc = new List<Account>();
         set<id> accountid= new set<id>();
         for(Account a:alist)
         {
           if(a.Opportunities.size()>2)
           {
               accountid.add(a.id);
               
               delacc.add(a);
           }
         }

List<Case> caselist=[select id from case where accountid in :accountid];

delete caselist;
         system.debug(delacc);
         delete delacc;

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Shiraz HodaShiraz Hoda

Hi Avinash,

Please use below code. Kindly mark as best answer if this resolves your issue.

List<Account> aList = [Select Name,(Select Name from Opportunities) from Account];
set<Id> setAccIds = new set<Id>();
List<case> caseListDel = new List<Case>();
         List<Account> delAcc = new List<Account>();
         for(Account a:alist)
         {
           if(a.Opportunities.size()>2)
           {
               system.debug(a.Opportunities.size());
			   setAccIds.add(a.Id);
               delacc.add(a);
           }
         }
		 for(case c: [select id from case where accountId IN: setAccIds]){
		 caseListDel.add(c);
		 }
		 
		 if(!caseListDel.isEmpty()){
		 delete caseListDel;
		 }
         if(!delacc.isEmpty()){
         delete delacc;
		 }
This was selected as the best answer