[SalesForce] After Insert Trigger on Email Message causing a problem in Case Assignment Rules

We have a after insert trigger on EmailMessage object to Capture "To Email address"
(routing address) in Email To Case(We have 10 routing address).

Whenever a Case gets created from Email to Case, in case history it shows like this.

  10/3/2012 11:28 AM  Surander  Changed Owner (Assignment) from  **Gallucci** to **Team1**.
                                Changed Owner (Assignment) from  **Surander** to **Galluci**.
                                Created.

But Surander is the Automated Case User, Gallucci is the Default owner of a case when assignment rules fail to locate an owner and Team1 is Queue.

But ideally it should not Change the Owner to Gallucci (If assignment rules locates
an Owner for Case).

If I inactivate the trigger it is working fine, and Case history shows like this.

10/3/2012 11:28 AM Surander Changed Owner (Assignment) from  **Surander** to **Team1**.
                            Created.

I need to write a workflow, when ever case owner is Gallucci, but for each and every Case workflow is firing because of the above issue.

Trigger Code:

Trigger ToAddress on EmailMessage (before insert)
{    
    List<Case> cases = new List<Case>();
    for (Integer i = 0; i < Trigger.new.size(); i++ )
    {          
        try
        {
            Case c = new Case();
            c=[SELECT id, Description, caseNumber FROM Case WHERE Id = :Trigger.new[i].ParentId];           
            c.To_Address__c= Trigger.new[i].ToAddress;           
            cases.add(c);            
        }
       catch(exception e)
       {
            System.debug('***********'+e);
       }  
    }   
    update cases;
}

We have assignment rules based upon To Email address. These email addresses are
updated by the trigger.

Rule 1 : Case: To Address  equals  kvat@symetrixsolutions.com        Team1
Rule 2 : Case: To Address  equals  hyvee@symetrixsolutions.com       Team2

Please reply with any coding or configuration that will help in this regard.Any
help is much appreciated.

Best Answer

I would reckon you need to specify the assignment rule to use use the Database.DMLOptions

Database.DMLOptions dmo = new Database.DMLOptions(); dmo.assignmentRuleHeader.useDefaultRule= true;

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

Related Topic