2

I'm wondering if anyone can help. I have an after insert trigger on a case that needs to check for the case's from email address and based on certain conditions perform different actions.

We have email-to-case functionality in place that automatically creates cases for our support team if the email is sent to our support@ address. In my test class I am trying to insert a case and associate it to an email message. I can't see how to do this though. I need to be able to run through the scenario of creating a case from an email.

Can anyone help me out please?

Many thanks.

3

You should be able to create an instance of your class that implements Messaging.InboundEmailHandler in the test method and then pass it a Messaging.InboundEmail and Messaging.InboundEnvelope. This will create the Case and associate it with the email.

E.g.

global class EmailToCaseHandler implements Messaging.inboundEmailHandler{ ... }


@isTest
private class EmailToCaseTest {
    static testMethod void testCaseFromEmail() {

        Messaging.InboundEmail email = new Messaging.InboundEmail();
        email.subject = 'test email-to-case';

        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        env.fromAddress = 'user@acme.com';

        EmailToCaseHandler emailHandler = new EmailToCaseHandler();
        emailHandler.handleInboundEmail(email, env);


        // Make assertions about the case that should be created.
    }
}

Maybe I misinterpreted what you were asking for... Do you just want to test a Case that is associated with a EmailMessage?

Here you would first insert the Case before inserting the EmailMessage. When inserting the EmailMessage, use the Case.Id field to populate the EmailMessage.ParentId field.


How can you find the EmailMessage that is associated with the Case via the EmailMessage.ParentId when the Case is being inserted.

My answer here is that you can't. The EmailMessage is inserted into the database after the Case is because it needs to reference the Case.Id in the ParentId field. See Trigger on Case EmailMessage

  • Hi Daniel, thanks for replying. My problem is that in my test class I'm inserting the case, then inserting the email with EmailMessage.ParentId=Case.Id, but inside my after insert case trigger when I query back the email message like so: List<EmailMessage> emails = [Select FromAddress, ParentId, Incoming from EmailMessage Where ParentId = :c.Id ]; the list is empty. I need to be able to get at the case's assoicated email to check the FromAddress of the email so that I can do certain actions based on the email domain. Thanks again Daniel. Brian. – Brian Casey Apr 10 '14 at 9:02
  • 1
    I think I see the problem. You want to find the EmailMessage that is associated with the Case via the EmailMessage.ParentId when the Case is inserted. I've extended my answer. – Daniel Ballinger Apr 10 '14 at 19:16
  • Hi Daniel, thank you very much for your help - I really appreciate it. I had a feeling alright yesterday that I would have to go down the route of writing a trigger on the EmailMessage object because I couldn't get the association to the email message no matter what I tried in the case trigger. Thanks again for your help and the very useful link to the example of a trigger on EmailMessage. – Brian Casey Apr 11 '14 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.