+ Start a Discussion
Kevin TullosKevin Tullos 

Delete open tasks when an account changes ownership

I was thinking that you could have a trigger like this on the account, but it didn't work.  Please help me with it.



/*
    Created by: Kevin Tullos
    Last Update: 22 June 2014 by Kevin Tullos
    Questions?: kevin.tullos@fleetpride.com
*/
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, Status, OwnerId FROM Tasks WHERE IsClosed = False) 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;
                    }
                } 
          
            }
           
        }
  
}
Best Answer chosen by Kevin Tullos
SarfarajSarfaraj
Hi Kevin

This one should work. What is the problem you are facing?

--Akram

All Answers

SarfarajSarfaraj
Hi Kevin

This one should work. What is the problem you are facing?

--Akram
This was selected as the best answer
Deepak Kumar ShyoranDeepak Kumar Shyoran
You can delete task by using above code but this code contain Delete DML  inside a for loop which will generate governor limit exception for bulk records.
Remove that from Loop and put those task which you want to delete in a list and then delte them outside from loop.
Kevin TullosKevin Tullos
btw...  akram was right.  It worked as it was.
Kevin TullosKevin Tullos
btw this is the final code...

trigger deleteOld_Owner_tasks on Account (after 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, Status, OwnerId FROM Tasks WHERE IsClosed = False) 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 != newOwnerId) { //if the task is assigned to the old account Owner
                    delete t;
                    }
                } 
          
            }
           
        }
  
}
SarfarajSarfaraj
Hi Kevin

Move the dml operation out of the loop.
if (!accountIds.isEmpty()) 
{ //if the accountIds Set is not empty
	for (Account act : [SELECT Id, (SELECT Id, Status, OwnerId FROM Tasks WHERE IsClosed = False) 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 != newOwnerId) { //if the task is assigned to the old account Owner
            	TaskUpdates.add(t);
        }
	}
	delete TaskUpdates;
}

--Akram