Is it possible to completely replicate in a unit test creating an outbound emailmessage record against a Case? By that I mean including the Task creation.

I've tried the below in a test class (and also in execute anonymous) but no related Task was created:

emailMessage outboundOne = new emailMessage(
ParentId = testCase.Id,
Subject = 'First outbound email subject',
TextBody = 'First outbound email textbody',
ToAddress = newContact.Id,
Status = '3',
Incoming = false  

Maybe this is handled via the EmailAuthor page and/or the URL parameters...which isn't reproducible in a unit test?

Some background: Basically there is a code based Roll up summary field for inbound/outbound email message - it's code based due to complicated logic around what emails to include in the count.

  • It's unclear to me exactly what you're trying to run this in the context of. Is there a page or workflow that creates the task from the email? Is this console based? But yes, one can create the email, however, from your description some other code needs to create the task based on the email and it's unclear to me what code should be doing that as you've not given the full context.
    – crmprogdev
    May 5, 2015 at 16:00
  • From within the standard Case UI (service console), with email2case enabled, when you send an email using the standard button a completed task is also created. However when attempting to create an outbound email with a test context (or even using execute anonymous) it isn't. While reviewing some test code I was expecting to see task triggers firing , didn't see any and then starting looking deeper...
    – Girbot
    May 5, 2015 at 16:04
  • You'd need to somehow set the console as the environment in your test class for that to happen. I've not worked with it, so can't guide you, but perhaps Customizing Case Feed with Code or even the Console Integration Toolkit might be of some help.
    – crmprogdev
    May 5, 2015 at 16:45
  • Girbot -- are you actually doing a DML insert of the variable outboundOne? Assuming yes, you may also need to insert the Task as part of the testmethod mocking.
    – cropredy
    May 5, 2015 at 23:48


You must log in to answer this question.

Browse other questions tagged .