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
stollmeyerastollmeyera 

When lead ownership is changed, prevent open tasks from being modified

When there is an open task assigned to a lead, and that lead is taken over by another user, the open task automatically gets assigned to that new owner.  I've looked around and there is nothing available from the "clicks" side of things to prevent this.  Has anyone else faced this problem and, if so, found a way to address it through code?

kkr.devkkr.dev

You cannot override this functionality by any configuration areas. You will need to write a trigger to replace new task owner with old owner.

Ritesh AswaneyRitesh Aswaney

trigger LeadAfter on Lead (After Update){

 

Lead[] newOwners = new Lead[]{};

 

for(Lead lea : trigger.new)

if(lea.OwnerId != trigger.oldMap.get(lea.Id).OwnerId)

newOwners.add(lea.Id); //aggregate all Leads whose Owners have changed

 

Task[] tasks = new Task[]{};

 

for(Task t : [Select Id, WhoId, OwnerId from Task where WhoId IN :newOwners]){

t.OwnerId = trigger.newMap.get(t.WhoId).OwnerId;  //grab the new Lead Owner and set as Task Owner too

tasks.add(t);

}

 

if(tasks != null && !tasks.isEmpty())

Database.update(tasks)

}

stollmeyerastollmeyera

Would I want to write the trigger on the Lead or on the Task? The way I see it, after taking ownership of the Lead it automatically performs an update on the task in the name of the current user.  Because of this, I have been trying to write something up on the Task update to put it back to the old assignee, but have been unsuccessful thus far.