You need to sign in to do that
Don't have an account?
Kevin 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;
}
}
}
}
}
/*
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;
}
}
}
}
}
This one should work. What is the problem you are facing?
--Akram
All Answers
This one should work. What is the problem you are facing?
--Akram
Remove that from Loop and put those task which you want to delete in a list and then delte them outside from loop.
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;
}
}
}
}
}
Move the dml operation out of the loop.
--Akram