Email Message Approval Process – Test

Email Message Approval Process – Test

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.

Solution:

Adam Created below apex test class:

Apex test Class:

@isTest(SeeAllData=true)

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 = ‘test@example.com’;

        em.FromAddress = ‘test-abc@twopirconsulting.com’;

        em.TextBody = ‘Some text’;

        em.Send_Template__c = ‘Draft Review’;

        em.Subject = ‘Test subject’;

        em.status = ‘5’;

        em.Incoming = false;

        INSERT em;

        Test.startTest();

        //Create an approval request for the email

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

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

        req1.setObjectId(em.Id);

        //Submit the approval request for the email

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

        //Verify the result

        System.assert(result.isSuccess());

        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<Id> 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.’);

        req2.setAction(‘Approve’);

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

        req2.setWorkItemId(newWorkItemIds.get(0));

        //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());

        System.assertEquals(

            ‘Approved’, result2.getInstanceStatus(), 

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

        // TODO: check feed

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

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

        Test.stopTest();

    }

}

Test the Business Case:

Test class run successfully and cover EmailMessageTrigger and EmailMessageHandler class.