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
JNicJNic 

EmailMessage Trigger not updating Case

Hi all,

 

I'm building an EmailMessage Trigger that is supposed to identify a case that was created by Email-To-Case feature and update a custom field called SuppliedToEmail__c with the ToAddress of the Email that has been invoked in this trigger.

 

My After Update is below:

trigger EmailMessageAfterUpdate on EmailMessage (After update) {
    for (EmailMessage newEmail : trigger.new){
    System.debug('******Entering EmailAfterUpdate******');

        if(Trigger.oldMap.get(newEmail.id).ParentId == null && newEmail.ParentId != null){
        Case c = [SELECT id, SuppliedToEmail__c FROM Case WHERE Id = :newEmail.ParentId];
            System.debug('******Entering EmailAfterUpdate\n'+c.CaseNumber+'******');
        c.SuppliedToEmail__c = newEmail.ToAddress;
        update c;
            System.debug('******Entering EmailAfterUpdate\n'+c.SuppliedToEmail__c+'******');
        }
    }
}

 

Unfortunately this trigger is not being invoked, I confirmed by ranning some tests and reviewing the debug logs.

 

I want to replicate the WebEmail (SuppliedEmail) field update, upon creation of a Email-to-Case record, to perform the same thing with my custom email field.

 

Your help is appreciated.

 

Best Answer chosen by Admin (Salesforce Developers) 
vhanson222vhanson222

The code below works for me:

 

 

trigger EmailMessageAfterUpdate on EmailMessage (after insert) {

    for (Integer i = 0; i < Trigger.new.size(); i++ )
    {
    System.debug('******Entering EmailAfterUpdate******');

        EmailMessage newEmail = Trigger.new[i];
        List<Case> c = [SELECT id, Description, caseNumber FROM Case WHERE Id = :newEmail.ParentId];
        if (c.size() > 0){
            System.debug('******Entering EmailAfterUpdate\n'+c[0].CaseNumber+'******');
        c[0].Description = newEmail.ToAddress;
            System.debug('******Entering EmailAfterUpdate\n'+c[0].Description+'******');
             update c;
        }
        
    }
   

}

 

of course, you will just have to adjust it to fit your scenario... my code was just a test to see if it worked.

 

All Answers

vhanson222vhanson222

The code below works for me:

 

 

trigger EmailMessageAfterUpdate on EmailMessage (after insert) {

    for (Integer i = 0; i < Trigger.new.size(); i++ )
    {
    System.debug('******Entering EmailAfterUpdate******');

        EmailMessage newEmail = Trigger.new[i];
        List<Case> c = [SELECT id, Description, caseNumber FROM Case WHERE Id = :newEmail.ParentId];
        if (c.size() > 0){
            System.debug('******Entering EmailAfterUpdate\n'+c[0].CaseNumber+'******');
        c[0].Description = newEmail.ToAddress;
            System.debug('******Entering EmailAfterUpdate\n'+c[0].Description+'******');
             update c;
        }
        
    }
   

}

 

of course, you will just have to adjust it to fit your scenario... my code was just a test to see if it worked.

 

This was selected as the best answer
JNicJNic

Below code works well Victor.

 

Thanks for your input.

vhanson222vhanson222

I should have also noted that I did not bulkify that code, so you should make sure to do that too.

 

 

trigger EmailMessageAfterUpdate on EmailMessage (after insert) {
    
    // create a list of cases so that we don't perform too many DML operations in our 'for' loop
    List<Case> cases = new List<Case>();
    for (Integer i = 0; i < Trigger.new.size(); i++ )
    {
    System.debug('******Entering EmailAfterUpdate******');

        EmailMessage newEmail = Trigger.new[i];
        List<Case> c = [SELECT id, Description, caseNumber FROM Case WHERE Id = :newEmail.ParentId];
        if (c.size() > 0){
            System.debug('******Entering EmailAfterUpdate\n'+c[0].CaseNumber+'******');
        c[0].Description = newEmail.ToAddress;
            System.debug('******Entering EmailAfterUpdate\n'+c[0].Description+'******');
             //update c;
             cases.add(c);
        }
    }
    update cases;
}

 

 

Glad i could help!