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
jamesmulcahyjamesmulcahy 

Trigger for when a new e-mail is added to a case


We have Email2Case configured to add inbound e-mails to their respective case when they arrive.

I'd like to write a trigger which fires when such an e-mail arrives (so that I can potentially move the case to a different state).  I can see two ways of almost achieving this, but I can't find all the pieces of the puzzle.

1. I could create a trigger for when a new task is created, except I can't seem to find a way of getting at the e-mail assocated with the task -- is there a way to do this?

2. I could use the InBoundEmail handler to handle the e-mail, but then I need to manually go about working out which case it relates to, which seems a little heavy handed -- is there a way to have SF give me the Case# for an e-mail if it spots the reference tag?

Is there a simple solution I'm overlooking here?

Cheers

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf
To elaborate, you must first enable Email To Case under Setup->Customize->Case->Email To Case.  This exposes the EmailMessage object.  Now you can create a workflow rule which is triggered on incoming Email Messages and which has a field update that updates the corresponding Case.

All Answers

jamesmulcahyjamesmulcahy
OK, I've got a little further with finding the EmailMessage associated with the task, but it's looking like I can't do it using a trigger this way.

The problem being that the EMailMessage hasn't been created at the point the task is created -- so trying to pull out all the Emails associated with this task always yields zero results.

The code I'm using right now is:

trigger EMailTrigger on Task (after insert) {
  for (Task aTask : Trigger.new)
  {
    EMailMessage[] m = [SELECT FromName, ActivityID from EmailMessage]; // WHERE ActivityID = :aTask.id];
    
    for (EmailMessage aMessage : m)
    {
      system.debug('Message from ' + aMessage.FromName + ' with ActivityID ' + aMessage.ActivityID + ' task = ' + aTask.id);
    }
  }
}

Debug from initial run:
20080530113222.675:Trigger.EMailTrigger: line 8, column 7: Message from James Mulcahy with Activity ID00TT0000008QMM8MAO task = 00TT0000008QNprMAG

Debug from followup run:

20080530113415.843:Trigger.EMailTrigger: line 8, column 7: Message from James Mulcahy with Activity ID00TT0000008QNprMAG task = 00TT0000008QNq5MAG
20080530113415.843:Trigger.EMailTrigger: line 8, column 7: Message from James Mulcahy with Activity ID00TT0000008QMM8MAO task = 00TT0000008QNq5MAG

Take note of how there is no e-mail matching the initial task ID until the 2nd run.

Is the InboundEmail route my only choice here?  If so, I'm still stumped on how to associate that with a case?
werewolfwerewolf
There is a simple solution.  Try writing a Workflow rule on EmailMessage.  That workflow will know what the parent case is and can act on it.
jamesmulcahyjamesmulcahy
lol - that couldn't have been easier!

Many thanks for your response!
ExtorcistExtorcist
Hi,

  I am a new user to saleforce, and trying to achieve the same. Appreicate if you could elobrate more on the setup workflow steps

Thanks        
werewolfwerewolf
To elaborate, you must first enable Email To Case under Setup->Customize->Case->Email To Case.  This exposes the EmailMessage object.  Now you can create a workflow rule which is triggered on incoming Email Messages and which has a field update that updates the corresponding Case.
This was selected as the best answer