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
Todd KadasTodd Kadas 

Approval Process test class unanimous approval required

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 acheive approval?

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

                        }
                 
                    }
               }
             }   
        }  
    }
test class
@isTest
private class ApprovalComments_Test{
 static testMethod void testMethodAppCmmt() 
  {
        Account acct = new Account(Name = 'Test Account');
        insert acct;

//Create your pricebook entry
// Instantiate the Pricebook2 record first, setting the Id
Pricebook2 standardPricebook = new Pricebook2(
  Id = Test.getStandardPricebookId(),
    IsActive=true

);

// Run an update DML on the Pricebook2 record
// This enables IsStandard to become true
// on the PricebookEntry record
update standardPricebook;

// Re-Query for the Pricebook2 record, for debugging
standardPricebook = [SELECT IsStandard FROM Pricebook2 WHERE Id = :standardPricebook.Id];

// This should return true now
system.assertEquals(true, standardPricebook.IsStandard, 'The Standard Pricebook should now return IsStandard = true');

// Create the Product
Product2 testProduct = new Product2(
  Name = 'Test Product', 
  IsActive = true
);
insert testProduct;

            PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPricebook.Id, Product2Id=testProduct.Id, UnitPrice=99);
    insert pbe;
        
// Create the PricebookEntry
PricebookEntry testPbe = new PricebookEntry(
  Pricebook2Id = '01so0000003MaLt',
  Product2Id = testProduct.Id,
  UnitPrice = 100,
  IsActive = true
);

insert testPbe;

// Re-Query the PBE
testPbe = [SELECT Id, Pricebook2.IsStandard FROM PricebookEntry where Pricebook2Id = '01so0000003MaLt'];

// Should also return true
system.assertEquals(false, testPbe.Pricebook2.IsStandard, 'The Standard Pricebook should return true from the PBE as well.');
        
         List<Opportunity> opps = new List<Opportunity>();
        
      Opportunity testOpportunity = new Opportunity(
      //Changed StageName to Qualification so that field values do not match process builder criteria.  This will cause test class to fail 
            StageName = 'Qualification',
              CloseDate=System.today().addMonths(1),       
            AccountId = acct.Id,
            OwnerId='0051J000005xUguQAE',
            Credit_Terms_Requested__c='More than 60 days',
            Name = 'Test Opportunity',
            pricebook2Id = testPbe.Pricebook2Id,
                  Amount=150000.0,
             Service_Line__c='FCL - Standard',
             Submit_to_Credit_Approval_Process__c=TRUE
        );
        insert testOpportunity;
        opps.add(testOpportunity);
         system.debug(LoggingLevel.FINE, '<content>');  
        system.debug(opps.size());

       // return acct;
        OpportunityLineItem op=new OpportunityLineItem (quantity=1.0,Opportunityid=opps[0].id,   PricebookEntryId=testPbe.Id, Product2Id = testProduct.Id,
TotalPrice=500000.0 ,CM__c=100.0, UoM__c='KG' ,volume__c=10.0);
      insert op;

          opps =[SELECT AccountId, Amount, Id, Name, CloseDate, StageName, Credit_Terms_Requested__c, Submit_to_Credit_Approval_Process__c, Service_Line__c,(SELECT Quantity, ListPrice,PriceBookEntry.UnitPrice, PricebookEntry.Name,
Estimated_CM__c,CM__c,  UnitPrice FROM OpportunityLineItems) FROM Opportunity where AccountId=: acct.Id ]; 
        //AccountId=: acct.Id ORDER BY CloseDate DESC LIMIT 10 ];
         
 User user1 = [SELECT Id FROM User WHERE Alias='aAbou'];
            
        // 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());
    }
}