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
Guilherme_MonteiroGuilherme_Monteiro 

how to test approval process

Hi, everyone!

I need to write a test class in apex, but, I'm stuck at the moment I need to send an Email message record to approval.

There's an active approval process, and both the case and email message triggers the entry criteria (even though, I have set to ignore them).

I'm getting a DML Statement error as if there wasn't a valid approval process.

System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process was found.: []

As you can see, on the screenshot highlighted all fields are being sent to the approval process request.

System Debug
@isTest private class sendEmailMessageToApprovalTest{
    @isTest static void approvalProcessEmailMessage(){
        
        //Creating a new case
        Case cs                    = new Case();
        cs.Subject                = 'My new case';
        cs.Activity_Type__c = 'Postal';
        cs.Type                     = 'Warranty';
        cs.SubType__c         = 'Equipment';
        cs.Case_Option__c   = 'Global';
        cs.Origin                    = 'Phone';
        cs.OwnerId                =  UserInfo.getUserId();
        insert cs;
        
        //Creating a new email message
        EmailMessage email = new EmailMessage();
        email.FromAddress  = 'test@test.com';
        email.Incoming     = True;
        email.ToAddress    = 'secondtest@test.com';
        email.Subject      = 'Test email';
        email.HtmlBody     = 'Test email body';
        email.ParentId     = cs.Id; 
        insert email;
        
        ProcessDefinition processDefinitionId = [SELECT Id
                                                                        FROM ProcessDefinition
                                                                       WHERE TableEnumOrId='EmailMessage'
                                                                       AND State='Active'
                                                                       LIMIT 1];
        
        // Create an approval request for the opportunity

       Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
       req1.setComments('Submitting request for approval.');
       req1.setObjectId(email.id);
       req1.setSkipEntryCriteria(true);
       req1.setSubmitterId(cs.OwnerId);
       req1.setProcessDefinitionNameOrId(processDefinitionId.Id);
        

       // Submit the record to specific process
 
 
       Approval.ProcessResult result = Approval.process(req1);
        
    }
}

I really appreciate your help.

Thanks in advance,


Guilherme