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
Zoren DomingoZoren Domingo 

apex trigger delete multiple record

Hi, can anyone help me creating apex trigger for deleting multiple records once a custom field is updated?
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger DeleteOnUpdate on Account (after insert, after update) {
    
    List<Account> accToDelete = new List<Account>();
    
    for(Account acc: Trigger.new){
        If (acc.Name == 'Khan'){
            // This Account matches your criteria, so we should add it to our list to delete
            accToDelete.add(acc);
        }
    }
    
    // List of Accounts we want to delete
    if( accToDelete.size()>0 ) { 
        delete accToDelete; 
    }
}

*Instead of Name you can take any custom field in if condition*


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Zoren
 
List<Account> accToDel=new List<Account>();
for(Account acc:trigger.new)
{
   if(acc.name__c=='delete')
   {
   accToDel.add(acc);
   }
}
delete accToDel;

Here we are looping the list of accounts that are updated and check a condition for field update.If the update matches the condition then we are deleting the record.

Hope this Helps.

Thanks
 
Zoren DomingoZoren Domingo

Hi I forgot to mention, 

the field that will be check is on the OpportunityLineItem
the record to be create is on another custom object related to opportunity

OpportunityLineItem >> Opportunity << Custom_object__c

Khan AnasKhan Anas (Salesforce Developers) 
Hi Zoren,

Please use below code:
 
trigger DeleteOnOppLineUpdate on Opportunity (after insert, after update) {
    
    set<Id> Oid = new set<id>();
    List<Opportunity> accToDelete = new List<Opportunity>();
    
    for(Opportunity acc: Trigger.new){
        Oid.add(acc.Id);
    }
    
    List<OpportunityLineItem> oli = [SELECT Id, Name, Discount, ProductCode FROM OpportunityLineItem WHERE Opportunityid =: Oid];
    for (Opportunity o: Trigger.New) {
        for(OpportunityLineItem ol : oli){
            if (ol.ProductCode == 'Khan') {
                accToDelete.add(o);
            }
        }
    }

    if( accToDelete.size()>0 ) { 
        delete accToDelete; 
    }    
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
Ajay K DubediAjay K Dubedi
Hi Zoren,

Below code can fullfill your requirements. Hope this will work for you.

trigger DeleteOpp on Opportunity(after insert, after update){

List<Opportunity> opportunityList = new List<Opportunity>();
for(Opportunity opp: Trigger.new){
   if(opp.name__c == 'abc')
   {
   opportunityList.add(acc);
   } 
   }
   if(opportunityList.size()>0)
   {
     delete opportunityList;
   }  
}

     
Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi