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
PeteMPeteM 

Simple Trigger To Delete Records

Hi,

 

I am new to Apex development and looking for some help with a simple trigger to delete erroneous opportunities created by an automated process.

 

Basically, we want to delete any opportunity that is created where the opportunity name = "..."

 

Thanks for any assistance!

Best Answer chosen by Admin (Salesforce Developers) 
PeteMPeteM

Thanks to all for the guidence....  here is the final code that appears to accomplish what I was looking to do.

 

trigger DeleteOpp on Opportunity(after insert, after update) {
 List<Id> lstId = new List<Id>();
 
for(Opportunity opp: Trigger.old){
        List<Opportunity> existoppList = [Select Id from Opportunity where Name =: '....'];
        delete existoppList;
    }
 
}

 

Thanks again,


Pete

All Answers

steve456steve456

trigger DeleteOpp on Opportunity__c(after delete) {

 List<Id> lstId = new List<Id>();

 

 if(Trigger.isDelete) {
        for(Opportunity opp: Trigger.old) {            
                lstId.add(opp.Id);
        }

 

 

if(Trigger.isDelete) {
        List<Opportunity__c> existoppList = [Select Id,All the fields you need from Opportunity where Id IN: lstId and Opportunity Name='*********'];
        delete existoppList;
    } 

 

}

 

Sample Code...May contain Errors...You can adjust as per your req.........

PeteMPeteM

Thanks Steve for the help!

 

I am stillhaving a problem....it does not seem to fire.  Here is a litlle more information.  We have a process that is creating a new opportunity, then we have a workflow rule closing it.  I would like the trigger to actually delete the record once it is created.  Below is how I attempted to modify your code example:

 

trigger DeleteOpp on Opportunity(after update) {
 List<Id> lstId = new List<Id>();
 
if(Trigger.isDelete) {
     for(Opportunity opp: Trigger.new) {           
              lstId.add(opp.Id);
       }
 
 
for(Opportunity opp: Trigger.new){
        List<Opportunity> existoppList = [Select Id from Opportunity where Name =: '...'];
        delete existoppList;
    }
 
}}

 

Thanks again for the assistance.

 

Pete

Devendra@SFDCDevendra@SFDC

Hi Pete, 

 

Couple of observations: you missed the "after delete" event  in trigger and you can not use Trigger.New in delete operation. Instead use Trigger.Old.

 

Apex Trigger Documentation: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers.htm

 

Thanks,

Devendra

Starz26Starz26

Could you just check the the value of name = "..." in the before insert, and the add an error to prevent it from inserting? Or is your automated process using all or none?

 

if the latter, you may have to set up a batch process to delete those records or:

 

create an @future method to delete ALL records where name = "...". call the @future method from the trigger.. (I beleive this will work as it is aSync but have not tried it.

PeteMPeteM

Thanks to all for the guidence....  here is the final code that appears to accomplish what I was looking to do.

 

trigger DeleteOpp on Opportunity(after insert, after update) {
 List<Id> lstId = new List<Id>();
 
for(Opportunity opp: Trigger.old){
        List<Opportunity> existoppList = [Select Id from Opportunity where Name =: '....'];
        delete existoppList;
    }
 
}

 

Thanks again,


Pete

This was selected as the best answer
Jason FlammangJason Flammang
This is a fairly old thread, but wanted to comment on the best answer given.

You DO NOT want to run CRUD operations or SOQL inside of a for loop as you will very quickly hit your limits.  Also, you don't want to use "trigger.old" as this is unavailable for insert triggers. Simple change.  See the example code below
trigger DeleteOpp on Opportunity(after insert, after update) {
    List<Opportunity> oppsToDelete = new List<Opportunity>();
 
    for(Opportunity opp: Trigger.new){
        If (opp.Name == '...'){
		    //  This opportunity matches your criteria, so we should add it to our list to delete
            oppsToDelete.add(opp);
        }
    }
	
    //  we now have a list of opportunities we want to delete
	if( oppsToDelete.size()>0 ) { delete oppsToDelete; }
}

 
dburks_gooseheaddburks_goosehead
Jason, is it possible to delete records before they're inserted or do you have to trigger an error to prevent them from being inserted?
Rajesh DhawanRajesh Dhawan
I get an error when I tried the above code as we cannot delete on Trigger.new and Trigger.old records.

I tried the below trigger with a Future method and it worked perfectly.

Trigger
trigger DeleteOpp on Opportunity(after insert, after update) {
               Set<Id> OppIds = new Set<Id>();
    for(Opportunity opp: Trigger.new){
        If (opp.Name == '...'){
                                  //  If this opportunity matches your criteria, we should add it to our Set of Ids to delete
            OppIds.add(opp.Id);
        }
    }
              
    //  We now have a list of opportunities we want to delete. Pass them on to the Future Method.
                              DeleteOpty.DeleteTheOpp(OppIds);
}

 
Apex Class with Future Method
public class DeleteOpty {
    @future
    public static void DeleteTheOpp(Set<Id> oppsToDelete){
     //We need to query as we cannot delete using Ids. We need a List of SObjects.
     List<Opportunity> optyList = [select id from Opportunity where Id=:oppsToDelete];
               if( oppsToDelete.size()>0 ) { delete optyList; }
    }
}