+ Start a Discussion
Elad Kaplan 3Elad Kaplan 3 

Email Message Status event

I want to update some fields on a case related to an email message, when the email message status changes, the problem is, that the status change doesn't triggers anything (trigger or workflow), I also can't create a custom object to look up to an Email message.

Anyone has a work around i can do ? 
CheyneCheyne
Why can't you write a trigger on the EmailMessage?

trigger ProcessStatusChanges on EmailMessage (after update) {
    for (EmailMessage m : trigger.new) {
        //check if the status changed
        if (trigger.newMap.get(m.Id).Status != trigger.oldMap.get(m.Id).Status) {
            //do stuff
        }
    }
}
CheyneCheyne
This post might be helpful to you as well:

http://salesforce.stackexchange.com/questions/16795/trigger-on-case-emailmessage (http://salesforce.stackexchange.com/questions/16795/trigger-on-case-emailmessage" target="_blank)
Elad Kaplan 3Elad Kaplan 3
Already tried it ..

The EmailMessage Status change, doesn't fire the trigger update event.. 

That's how the trigger looks like :

trigger trg_EmailMessage on EmailMessage (before insert, after insert, before update, after update, before delete, after delete) {

cls_TriggerFactory.createHandler(cls_TriggerFactory.TriggerHandler.TH_EMAIL_MESSAGE);
}

The trigger calls a class that performes the logic, however, as I've mentioned, the trigger doesn't get fired on EmailMessage update event..
CheyneCheyne
Can you post your class?
Elad Kaplan 3Elad Kaplan 3
That's the method that's suppose to update a field in the related Case(The Email is Email2Case):


public void setUnreadEmailFlagTrue(EmailMessage message)
{  
  if (message.Status != null && message.Status.EqualsIgnoreCase('0')
  && message.Incoming != false && message.ParentId != null)
  {  
   String ParentIdStr = String.valueOf(message.ParentId);
  
   if(ParentIdStr.startsWith('500'))
   {    
    Case theCase = new Case(Id = message.ParentId);
    theCase.Has_Unread_Emails__c = true;
    db_manager.append(theCase);
   }
  
  }
}
CheyneCheyne
Your trigger calls this method

cls_TriggerFactory.createHandler(cls_TriggerFactory.TriggerHandler.TH_EMAIL_MESSAGE);

but you have not posted it. Could you post all of the code that this trigger runs?
abdelaziz leibekabdelaziz leibek

@Elad Kaplan 3

Have you find the answer to your problem?
thks
Kaplan EladKaplan Elad
abdelaziz leibek , I've ended up creating a scheduler that runs every few hours and goes over all the Email Messages with the desired status and where soeme Checkbox is false and changes the parent Case field, then checking the checkbox true
abdelaziz leibekabdelaziz leibek
@Kaplan Elad ok thks