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

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!
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
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.........
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
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
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.
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
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
I tried the below trigger with a Future method and it worked perfectly.
Trigger
Apex Class with Future Method