+ Start a Discussion
tlo300tlo300 

Salesforce keeps reassigning Case owner from Email2Case

Hi,

 

I have created a trigger on he EmailMessage object that checks the address an Email was sent to and uses that Address to match it with the Users in Salesforce that have their own personal address (a custom field) entered. When a match is found it will change the ownership of the Case that was created by Email2Case to that particular person.

 

The case is created, assignment rules change ownership to a queue. Then my after trigger changes ownership again to the correct person.

 

Now here is the strange part. After this Salesforce changes ownership of the Case to the Standard Case owner User that is specified under Case support settings if Salesforce was unable to assign a case.

 

Does anyone know why this happens? Why does this rule apply? If i look at a case history it first has an owner from the assignment rules, then an owner from my trigger and still Salesforce seems to think it was unable to assign an owner to the case and changes it back to the default setting..

Best Answer chosen by Admin (Salesforce Developers) 
tlo300tlo300

If anyone is interested, we fixed it by executing our code in a class with the @future annotation.

All Answers

Ritesh AswaneyRitesh Aswaney

Working on a hunch here, reckon this is assignment rules kicking in

 

Here's a bit about assignment rules

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database_dmloptions.htm

 

I would try using a DMLOption in your update to prevent the default rule firing, i.e. something like this

 

 

 

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= false;
case.setOptions(dmo)
Also, you're doing this in a before insert trigger ?

 

 

tlo300tlo300

I am using an after trigger on the EmailMessage object.

tlo300tlo300

I added a check on the Case before trigger that set up the options like you specified when a case will be created from email2case. It still just reassigns ownership after everything is done

 

I also tried creating a dummy owner and add it to the before case trigger. That way it would have an owner, the default owner thing wouldn't pop up and I just overwrite the owner with the after trigger. Still, salesforce puts the default owner on the case -_-

tlo300tlo300

If anyone is interested, we fixed it by executing our code in a class with the @future annotation.

This was selected as the best answer
Lucky19Lucky19

Could you please explain how it is achieved  using Future annotation

Andries.NeyensAndries.Neyens
I found a different workaround :

Create a Case Assignment rule with a rule that always triggers (for instance the Case OwnerID not empty
And check the field: Do Not Reassign Owner