0

I have a trigger that grabs approval comments and updates a custom field on an opportunity record. I am able to get 100% code coverage on test class when approval process requires one approver.

However, when I change approval process to one that requires unanimous approval, I get the following error: System.AssertException: Assertion Failed: Instance StatusPending: Expected: Approved, Actual: Pending.

Any idea how to update test class to, I suppose, send to parallel approvers, and achieve approval?

Here's the approval process portion of test class

    // Create an approval request for the opportunity
    Approval.ProcessSubmitRequest req1 = 
        new Approval.ProcessSubmitRequest();
    req1.setComments('Submitting request for approval.');
    req1.setObjectId(testOpportunity.id);

    // Submit on behalf of a specific submitter
    req1.setSubmitterId(user1.Id); 

    // Submit the record to specific process and skip the criteria evaluation
    req1.setProcessDefinitionNameOrId('Credit_Legal_and_Strategy_Review');
    req1.setSkipEntryCriteria(true);

    // Submit the approval request for the account
    Approval.ProcessResult result = Approval.process(req1);

    // Verify the result
    System.assert(result.isSuccess());

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

    // Instantiate the new ProcessWorkitemRequest object and populate it
    Approval.ProcessWorkitemRequest req2 = 
        new Approval.ProcessWorkitemRequest();
    req2.setComments('Approving request.');
    req2.setAction('Approve');
    req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

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

    // Verify the results
    System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
    // This fails when approval process requires unanimous approval
    System.assertEquals(
        'Approved', result2.getInstanceStatus(), 
        'Instance Status'+result2.getInstanceStatus());
}

}

and here is the trigger

trigger TriggerApprover on opportunity (before update) {

   if(trigger.isUpdate){
         List<Opportunity> opptyList =  [Select id,
                                               (Select Id, 
                                                     IsPending, 
                                                     ProcessInstanceId, 
                                                     TargetObjectId, 
                                                     StepStatus, 
                                                     OriginalActorId, 
                                                     ActorId, 
                                                     RemindersSent, 
                                                     Comments, 
                                                     IsDeleted, 
                                                     CreatedDate, 
                                                     CreatedById, 
                                                     SystemModstamp 
                                                FROM ProcessSteps
                                            ORDER BY CreatedDate DESC) 
                                                From opportunity
                                            WHERE Id IN : Trigger.new];

         if(opptyList.size() > 0){

           for(Opportunity opp : opptyList){

            for(Opportunity opp1 : Trigger.new) {

                     //check copy comment is true
                     if(opp.id == opp1.id && opp1.copy_comment__c) {

                       if (opp.ProcessSteps.size() > 0) {

                     opp1.Approval_History_Comments__c = opp.ProcessSteps[0].Comments;
                     //opp1.copy_comment__c = false;//

                       }

                    }

                }
           }
         }   
    }  
}

any help would be greatly appreciated. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.