Find your content:

Search form

You are here

Test class for mimicing email to case functionality

 
Share

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.


Attribution to: Brian Casey

Possible Suggestion/Solution #1

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


Attribution to: Daniel Ballinger
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/32320

My Block Status

My Block Content