You need to sign in to do that
Don't have an account?
APEX Trigger causing Case Assignment Rules to re-fire
I have an APEX Trigger that runs when a Task is created/updated, so it can update the related Case.
Somehow, the trigger is causing the Case Assignment Rules to re-fire, but I don't know what. Any thoughts?
trigger caseLastActivityDate on Task (after insert, after update) { Map<String, Task> tMap = new Map<String, Task>(); for (Task t : System.Trigger.new) { String whatId = t.WhatId; System.debug('whatId is ' + whatId); if(whatId <> null && whatId.startsWith('500') == true) { Case c = [select Id from Case where Id = :whatId]; if(c.Id <> null) { c.Last_Activity_Date__c = t.LastModifiedDate; update c; } } } }
The solution I had for this was two-fold:
First, we added a new field, "Case Taken", which is just a boolean. Once the case is taken by a user (who is not the default assignee), Case Taken is set to true.
Second, we updated the assignment rules with an new entry/step where if Case Taken equals True and the owner is not the default assigner, use the "Do Not Reassign Owner" flag.
All Answers
It probably has something to do with the order in which Apex executes. Check out the order of execution and see if you can track down some other triggers that are causing changes or perhaps workflow updates that are kicking off the trigger to fire again.
Jeff Douglas
Appirio, Inc.
http://blog.jeffdouglas.com
Thanks for the reply, Jeff.
The odd thing is that we have had numerous triggers running on before/after insert/update actions on the Case records themselves, and these have never caused the Assignment Rules to re-fire. Only the recent addition of the trigger on the Task has this happened. Reviewing the "Order of Exectution" doesn't provide an explanation.
Thus far, SF Premier Support has not been able to provide any explanation on why this is happening on the Task but not the Case.
I'm currently toying with the idea of putting in a hack for our Assignment Rules just so we can set the trigger live again (I've removed it since the re-assignment was causing problems for us), but am hoping there is an explanation & solution around this first.
Thank you
I'm having the exact same problem, but my trigger is on the EmailMessage object. Everytime a new email message gets attached to an existing Case (through Salesforce's Email-to-Case mechanism), the Case Assignment Rules run again, and the Case is taken from the owner and put back into the queue. Here's my trigger code:
Did you ever find a solution to this problem?
-- Rob
The solution I had for this was two-fold:
First, we added a new field, "Case Taken", which is just a boolean. Once the case is taken by a user (who is not the default assignee), Case Taken is set to true.
Second, we updated the assignment rules with an new entry/step where if Case Taken equals True and the owner is not the default assigner, use the "Do Not Reassign Owner" flag.
Thanks - I'll implement a similar fix. It would be nice to know why the Assignment Rules are re-run in these scenarios when they are not in others. We have a trigger on CaseComment that does the exact same thing as my EmailMessage example, but it doesn't cause the Assignment Rules to re-run.
In any event, I appreciate you following up with details on how you solved the problem.
-- Rob
I have a similar problem where when a User closes the case, a second Email is sent to Queue members from his own Email Account and the content of this email are same as of New Email Notification that is used to sent to Queue member at the time of Case Assignment to Queue when the case is created for the first time. This means case assignment rules are firing here again.
Actually we are using before Update trigger on Case to Change Case Owner to Queue (one from which it is accepted) once it is closed. I think this switch of case owner through this trigger is causing the Case Assignment to Refire. As per my knowledge, they should fire only once when the case is created.
I will appreciate if someone suggest how to stop this recursive firing of Case Assignment Rules.
I opened a case with Salesforce on the assignment rules firing twice because it's causing some of my cases to be assigned to the wrong queues and it was communicated to me it was a bug that would take a major release to fix (and not Summer '10).
I also forwarded them this post as well to show other customers were experiencing similar issues.
Same problem here - using an afterinsert trigger on casecomment to update a case. I'm glad I found this thread - I thought I was losing my mind.
I'll be implementing a similar solution, setting a flag to trigger an assignment rule with 'do not reassign owner' checked.
Bug is scheduled to be fixed in Winter '11
Experiencing the same bug using the rest api upserts. If you do a rest upsert to update an existing Case it will re-trigger assignment rules. In our example, this is happening when posting json data to the rest api and not appending any extra headers for assignment.
The work-around we use:
Just add a first line to the assignment rules to not change the owner if it's not new:
We have the same issue occurring when a Trigger on a custom object updates a field on Case. Even resolved cases are being run through assignment rules.
We fixed by refactoring assignment rules as formulas and adding ISNEW() to the criteria.
Bug is fixed now! you can now use it without any issues and errors!
https://aostvapk.fun/
I also am encountering this. Is this fixed? What is the resolution?
Let’s say after sometime, requirement came to calculate and update field on Vehicle object. Considering timeline to deliver this requirement, team decided to choose Workflow field update over modifying existing trigger. Consider that workflow rule is written to execute every time when record is either created or updated.
Now, whenever any record of custom object “Vehicle__c” is updated or created, chances that trigger will execute will be “four times” because of sequence of execution.
When record is created or updated trigger will execute twice (before and after event) and after that workflow field update will cause trigger to execute again (before and after event).
Trigger, Workflow and Process builder field update
To make situation worst, if we write field update on same object “Vehicle” in Process builder also then trigger may execute “six times” instead of only two times kgtopg (https://kgtopg.in/" target="_blank)