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
Kevin TullosKevin Tullos 

Delete open tasks when an account changes ownership

I want to delete all the open tasks that are associated with an account when the account owner is changed. I would post the code that i have written, but none of it is useful.

it can either be batch apex or a trigger.

Thanks.

Kevin
Best Answer chosen by Kevin Tullos
Naveen Rahul 3Naveen Rahul 3
you can create rule to change the satus of  open cases to closed and create a trigger to delete them,you cant directly create/delete  records from workflow rules.

rather you can update the field status and act upon them.

All Answers

Naveen Rahul 3Naveen Rahul 3
you can create rule to change the satus of  open cases to closed and create a trigger to delete them,you cant directly create/delete  records from workflow rules.

rather you can update the field status and act upon them.
This was selected as the best answer
Kevin TullosKevin Tullos
Thanks for the answer.  Yes.  I know that you cannot do it with a workflow rule.  I am trying to do it with a trigger or batch apex.  

I was thinking that you could have a trigger like this on the account...

trigger deleteOld_Owner_tasks on Account (before update) {
   
        Set<Id> accountIds = new Set<Id>(); //set for holding the Ids of all Accounts that have been assigned to new Owners
        Map<Id, String> oldOwnerIds = new Map<Id, String>(); //map for holding the old account ownerId
        Map<Id, String> newOwnerIds = new Map<Id, String>(); //map for holding the new account ownerId
        Task[] TaskUpdates = new Task[0]; //Task sObject to hold OwnerId updates
       
        for (Account a : Trigger.new) { //for all records
            if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId); //put the old OwnerId value in a map
                newOwnerIds.put(a.Id, a.OwnerId); //put the new OwnerId value in a map
                accountIds.add(a.Id); //add the Account Id to the set
            }
        }
       
        if (!accountIds.isEmpty()) { //if the accountIds Set is not empty
            for (Account act : [SELECT Id, (SELECT Id, OwnerId FROM Tasks) FROM Account WHERE Id in :accountIds]) { //SOQL to get Contacts and Opportunities for updated Accounts
                String newOwnerId = newOwnerIds.get(act.Id); //get the new OwnerId value for the account
                String oldOwnerId = oldOwnerIds.get(act.Id); //get the old OwnerId value for the account
               
                for (Task t : act.Tasks) { //for all tasks
                    if (t.OwnerId == oldOwnerId) { //if the task is assigned to the old account Owner
                    delete t;
                    }
                } 
          
            }
           
        }
  
}


It did not work.