You need to sign in to do that
Don't have an account?
Stopping Automatic Transfer of Open Activity Ownership (Related to the Account)
Hello,
I am wrote this really simple trigger to stop the changing of the ownership of open activities. Whenever account owner changes in SF, it changes all of the open activitiy owners of tasks to the new owner of the account. We don't want that to happen. Here's my code:
trigger StopTaskOwnerChange on Task (before update) { List<Task> updateTask = new List<Task>(); for(Task t : trigger.new){ if(t.ownerId != t.CreatedById) updateTask.add(t); } for(Task t : updateTask){ t.ownerId = t.CreatedById; }
I am thinking this problem with this is probably has to do with timing of the execution of the trigger. I know salesforce automatically changes all of the activity ownership automatically, and I am wondering if I'm firing my trigger before salesforce automatically changes the Owners so it looks the owner is not changing.
Hi ,
Please try in After update like
and update task as you want.
If your are not clear for this solution please let me know.
--
Regards
Usman
I think so, this is what I have now with your changes:
I did some manual testing and it still changes the task if the Account Owner owns the task. For example, I own XYZ Account and have one open activity. Another rep, Jane, has an open activity too on this account. Let's say I change the ownership of this XYZ Account to John. When I tested it, Jane's task stayed with her, which is correct, but my open task assigned to me switched over to John's ownership. Is there a way that I can stop that from happening too?
check this and let me know if it works
trigger preventTaskOwnerChange on Account ( after update)
{
list<task> updatetask = new list<task>();
for(account a :trigger.new)
{
string oldownerid = trigger.oldmap.get(a.id).OwnerId;
string newownerid = a.ownerid;
if(oldownerid != newownerid)
{
for( account acc : [ select OwnerId, ( select OwnerId from tasks ) from account])
{
for( task tsk :acc.tasks)
{
tsk.ownerId = oldownerid;
updatetask.add(tsk);
}
}
update updatetask;
}
}
}
Add a new custom field on Task (Activities): Last_Assigned_Owner__c (lookup to user or text(18))
Add in a Trigger for before insert and before update to keep this new field updated:
Add in Trigger for Case after update to detect owner change and restore all open tasks:
I hope this helps
Thanks for your source.
please could you show me the way,
how to add this source to what class?
I am adding [Last_Assigned_Owner] field to New Task (Activity) Page and want to assign owner from that specific Last_Assigned_Owner after saving or updating task.
best regards,
Lin Thaw.
select "Task Triggers" then click the new button and paste in this code
That will ensure that anytime the task is updated or created the current owner is saved away, changing the owner on a parent of the task (case or account etc does not fire this code it would seem).
Click Save.
Next from the quick search find the "Case Triggers", click the New button and paste in this code
Click the Save button and you are pretty much done apart from test classes to cover this, naturally if you had triggers for Task or Case already you would try to add them to those...
Thanks for previous answer.
I have another related question.
When a user creating new task (from Open Activities), is it possible to create only one task (not copies) that can be edited and closed by all users (but not more than 100 users) who are from one specific role.
Can I do that by using Trigger and Case?
Could you please share if you have some solution about it.
Currently, [Assigned To] [Multiple Users] (Lookup) function is creating individual copy tasks for each user. So I want to use above logic.
best regards,
Lin Thaw
And also want to offer this test class:
Thank you for the Code, Apex class works fine but the Test class is not working can you please confirm.
Regards,
Jeevana.
I was just confirming in my dev org that this test still runs and passes, your org may be different and it may need to be modified. What error are you getting in the test class in your org?
Thank You!