You need to sign in to do that
Don't have an account?
Kevin 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
it can either be batch apex or a trigger.
Thanks.
Kevin
rather you can update the field status and act upon them.
All Answers
rather you can update the field status and act upon them.
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.