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
Matt CzugalaMatt Czugala 

Creating 1st Trigger - Tasks Object

We have recently started using a plugin to automatically log calls when a call is received.  A small nuance we have noticed is that it logs the call even if the sales rep is not at their desk to answer the call.  This has resulted in thousands of tasks being unncessarily created.  So I am wanting to create a trigger to automatically delete one of these tasks once it has been created.
I just created a sandbox so that I start working this, but is there a how-to I can follow to try and complete this?  I know the criteria I need to match against so as long as the coding is similar to VB or SQL I should be able to complete.

Thanks!
Andy BoettcherAndy Boettcher
Matt,

I have a blog post that can get you started.  Read through it and poke me back here if you have questions!  Good luck!

http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/
Matt CzugalaMatt Czugala
I read through your blog post, it looks like a good starting point.  I will start developing tonight and see where it goes.
Thank you.
Andy BoettcherAndy Boettcher
I'm an old VB/SQL guy who came to the Force.com Platform.  If you get stuck on something just shoot me a note.  :)
Matt CzugalaMatt Czugala
Ok, I keep getting errors.  This seems so easy yet I'm slamming my head against the wall.  It's probably something so trivial.

I keep getting the following error:Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger DeleteBadLoggedCalls caused an unexpected exception, contact your administrator: DeleteBadLoggedCalls: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.DeleteBadLoggedCalls: line 4, column 1

trigger DeleteBadLoggedCalls on Task (after insert, after update){
    List<id> lstid = new List<id>();
    List<Task> existtasklist = new List<Task>();
    for(Task T: Trigger.old){
    if(T.type == 'Call' && T.CallType =='Inbound' && T.CallDurationinseconds < 10 && T.Quality_of_Call__c=='' ){
        existtasklist.add(t);
       } 
        }delete existtaskList;
}



Andy BoettcherAndy Boettcher
Hi Matt!

In the "insert" context, there is not a "trigger.old", only a "trigger.new".  =)

You can split up the logic to something like:

if(trigger.isInsert) {
     for(tast T : trigger.new) {
         if......
     }
}

if(trigger.isUpdate) {
     for(tast T : trigger.old) {
         if......
     }
}


Matt CzugalaMatt Czugala
Odd, I was following several different references to use .old.  IE (http://www.infallibletechie.com/2013/01/code-coverage-for-task-trigger.html)
Andy BoettcherAndy Boettcher
Read up on this blog post - this quickly explains the different components and whatnots of available collections and variables in a trigger:

http://techman97.wordpress.com/2012/07/18/how-to-write-a-basic-apex-trigger/
Matt CzugalaMatt Czugala
Well I have my trigger resaved and I am not receiving any errors, however the trigger does not seem to be deleting the task that matches the critera I set.

Example Task for Trigger to delete
Matt CzugalaMatt Czugala
I think I am very close to having this correct, but it just doesn't seem to be delete tasks.  Maybe I have the before/after update/trigger setup wrong?

trigger DeleteBadLoggedCalls on Task (after insert) {
    List<Task> lstTaskdelete = new List<Task>();
    List<Task> lstTask = [select id,type,CallType,CallDurationinseconds,Quality_of_Call__c from Task where id in: 
    trigger.newmap.keyset()];
    for(Task tk: lstTask ){       
        if(tk.type == 'Call' && tk.CallType =='Inbound' && tk.CallDurationinseconds < 22 && tk.Quality_of_Call__c==''){           
           lstTaskdelete.add(tk);
        }       
    }
    if(lstTaskdelete.size() > 0){
        delete lstTaskdelete;
    }
}


Andy BoettcherAndy Boettcher
I would throw in a System.Debug(lstTaskDelete); on line 10 to see if there is anything being put in that list - my *guess* is that one of the conditional statements on line 6 may be excluding what you're trying to delete?  (maybe tk.Quality_of_Call__c==null?)

-Andy
Matt CzugalaMatt Czugala
Quality_of_Call__C is a picklist.  I wasn't sure if I needed NULL, '', or ' ' for blank values.