+ Start a Discussion
anvesh@force.comanvesh@force.com 

How to delete a record when Trigger fired?

when field met certain value  then that record  should need to  delete...is this  possible  through  trigger....

 

My requirement is  i have field  Activity_Type__c ='  ' (is  no value)..then this record should be deleted. so  i written this trigger but  not working.

 

trigger DeleteTask on Task (after insert) {

task t= new task();
if(t.Activity_Direction__c==''){
task t1=t.clone();
t.remove(t1);
}

}

 

Please anyone have sollution?

Best Answer chosen by Admin (Salesforce Developers) 
hitesh90hitesh90

Hi anvesh,

 

Here is the sample code as per your requirement.

 

Apex Trigger:

trigger DeleteTask on Task (after insert) {
    List<Task> lstTaskdelete = new List<Task>();
    List<Task> lstTask = [select id,Activity_Direction__c from Task where id in: trigger.newmap.keyset()];
    for(Task tk: lstTask ){        
        if(tk.Activity_Direction__c==null || tk.Activity_Direction__c==''){            
           lstTaskdelete.add(tk);
        }        
    }
    if(lstTaskdelete.size() > 0){
        delete lstTaskdelete;
    }
}

 


Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/