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
Susheel Reddy BSusheel Reddy B 

APEX Trigger on Email Message causing a problem in Case Assignment Rules

Hi All,

 

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 inactive 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 (after insert) {    
    List<Case> cases = new List<Case>();
    for (Integer i = 0; i < Trigger.new.size(); i++ )
    {          
        try
        {
        EmailMessage newEmail = Trigger.new[i];
        List<Case> c = [SELECT id, Description, caseNumber FROM Case WHERE Id = :newEmail.ParentId];
        if (c.size() > 0 ){            
        c[0].To_Address__c= newEmail.ToAddress;           
             update c;            
         }  
       }
       catch(exception e)
        {
            System.debug('***********'+e);
        }  
       }   
          update cases;
}

 

 

Please reply with any coding or configuration that will help in this regard.

Any help is much appreciated.

 

Regards,

Susheel Reddy

SammyComesHereSammyComesHere

Can we get a look at assignment rules you have configured ?

Susheel Reddy BSusheel Reddy B

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
SammyComesHereSammyComesHere
Try this and let me know. You are trying to make a blank update as update cases.

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

Thanks for your reply, but still it shows the same and Workflow is firing.

SammyComesHereSammyComesHere

Is there a Trigger on Cases before or after which might be resetting the value and may be put the debug logs and check.

Logically it should have worked as Workflows would run after assignment rules.

Susheel Reddy BSusheel Reddy B

We don't have any triggers on Cases, but we have two triggers on Attachmnets object.

I will try with adding some dubug logs.

 

Again thanks so much.

Praveen KimarPraveen Kimar

 

Hi Susheel,

 

When you get an opportunity, please try the below code and let me know if it works.

 

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;
}

 

Thanks,

Praveen K.