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
Devon SacksDevon Sacks 

Writing a Test Class for a Email Apex Trigger

I currenlty have a trigger that when someone uses an Objecct button to send an email the Trigger updates the a custom field in the Case Object.
I have yet to write a script for email so I am not sure on this one. I have other triggers that after that field gets updated they update custom objects attached to the Case. Currently after sending an email they manually check the box (sometimes they forget so I want this automated).

Please see below current Trigger code.

trigger UpdateCaseEmail on EmailMessage (after insert)
{
    set<Id> caseIds= new Set<Id>();
    for(EmailMessage rmaEmail:Trigger.new)
    {
        caseIds.add(rmaEmail.ParentID);
    }

    // Find all cases related to the e-mail that just got sent and that have not
    // had their RMA task set yet.
    List<Case> csList = new List<Case>();
    for(ID thisEmailParentId : caseIds)
    {
        csList.add([SELECT Update_RMA_Task__c, CaseNumber, Id 
                    FROM Case 
                    WHERE Update_RMA_Task__c = False 
                    AND id = :thisEmailParentId]);
    }

    // Modify the cases to meet the business rule
    for(Case cs : csList)
    {
        cs.Update_RMA_Task__c = True;
    }
    
    // Send our updates back to Salesforce
    if(csList.size() > 0)
    {
        update csList;
    }
}
 
SonamSonam (Salesforce Developers) 
The following thread talks about the testing the EmailMessage trigger:
https://developer.salesforce.com/forums?id=906F00000008ytoIAA 
Devon SacksDevon Sacks
From the link I was able to write my own verion of the code, Thanks I hope this helps anyone else trying to do the same


@isTest
public class UpdateCaseEmail 
{
    Static testMethod void UpdateCaseEmail()
    {
        //Creating the account for testing
        account a1 = new account(name = 'test'); 
        Insert a1;
        //Creating the Contact w/email
        contact con = new contact(lastname ='testing', firstname = 'testcontact',email='testingit@gmail.com', accountid = a1.Id);
        Insert con;
        //Creating a case with the required fields to meet trigger rule(ie Case must have Update RMA task = to false)
        case cs = new Case(Account = a1, Contact = con, Status = 'Open', Origin = 'Inbound Phone Call', Subject = 'testing for apex class', 
                           Description = 'this needs to be a long description', Update_RMA_Task__c = False);
        Insert cs;
        //Sending an email to meet Trigger rule (ie must be assigned to case)
        EmailMessage emess = New EmailMessage(FromAddress ='dsacks@control4.com.dev', ToAddress= 'testingit@gmail.com', Subject = 'Testing Apex trigger',
                                              TextBody = 'this his the message of the email', ParentId = cs.Id);
        Insert emess;
        
    }
    

}
SonamSonam (Salesforce Developers) 
Glad to know the sample code helped!