Email Message Approval Process – Test Class

Email Message Approval Process – Test Class

Creating apex test class for Email Message Approval process.

Business Case:

Adam using approval process for Email Message standard object and he has implemented apex trigger to create case feed record with email message details for every approval or rejection. He wants to create test class for Email Message Approval process.


Adam Created below apex test class:

Apex test Class:


public class EmailMessageTriggerTest {

    static testMethod void OnAfterUpdate() {

        Case c = new Case();

        c.Status = ‘New Query’;

        INSERT (c);

        EmailMessage em = new EmailMessage();

        em.RelatedToId = c.Id;

        em.ToAddress = ‘’;

        em.FromAddress = ‘’;

        em.TextBody = ‘Some text’;

        em.Send_Template__c = ‘Draft Review’;

        em.Subject = ‘Test subject’;

        em.status = ‘5’;

        em.Incoming = false;

        INSERT em;


        //Create an approval request for the email

        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();

        req1.setComments(‘Submitting request for approval.’);


        //Submit the approval request for the email

        Approval.ProcessResult result = Approval.process(req1);

        //Verify the result


        System.debug(‘TEST: approval process start, result = ‘ + result);

        System.assertEquals(‘Pending’, result.getInstanceStatus(), ‘Instance Status ‘ + result.getInstanceStatus());

        //Approve the submitted request.  

        //First, get the ID of the newly created item

        List newWorkItemIds = result.getNewWorkItemIds();

        System.debug(‘TEST: newWorkItemIds = ‘ + newWorkItemIds);

        //Instantiate the new ProcessWorkItemRequest object and populate it

        Approval.ProcessWorkItemRequest req2 = new Approval.ProcessWorkItemRequest();

        req2.setComments(‘Approving request.’);


        //Use the ID from the newly created item to specify the item to be worked


        //Submit the request for approval

        Approval.ProcessResult result2 = Approval.process(req2);

        System.debug(‘TEST: approval process Approve, result2 = ‘ + result2);

        //Verify the results

        System.assert(result2.isSuccess(), ‘Result Status: ‘ + result2.isSuccess());


            ‘Approved’, result2.getInstanceStatus(), 

            ‘Instance Status’+result2.getInstanceStatus());

        // TODO: check feed

        List feed = [SELECT Id, Status, ParentId, Title, Body FROM FeedItem WHERE ParentId =: c.Id ORDER BY CreatedDate DESC];

        System.debug(‘feed after approval = ‘ + feed);




Test the Business Case:

Test class run successfully and cover EmailMessageTrigger and EmailMessageHandler class.