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

How to simulate approval process in trigger test method?

I have written a trigger that extracts certain totals from a custom object when the record is approved and updates some fields on the parent account.  This works fine in the sandbox.  However, I don't know how to go through the approval process from my test method (it is not done in Apex but from the  workflow approval engine in salesforce).  Can anyone explain or show me some sample test method code to help with this?  There is a snippet in the Apex Guide, but it is a single step approval process and our process is a two step process, so I need to:

- submit it for credit approval

- have the credit approver approve it

It then should move on to director approval and I need to

- have the director approve it.


So far, I have the following relevant code in the test method (I've set the data etc up earlier in the test method but not included it here so there is less to wade through):


		// Create an approval request for the JIS     
		Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
		req1.setComments('Submitting request for approval.');

		// Submit the approval request for the JIS  
		Approval.ProcessResult result1 = Approval.process(req1);      

		// Verify the results  
		System.assertEquals('Pending', result1.getInstanceStatus(), 'Instance Status'+result1.getInstanceStatus());

		// Approve the submitted request      

		// First, get the ID of the newly created item    
		List<Id> newWorkItemIds = result1.getNewWorkitemIds();
		// Instantiate the new ProcessWorkitemRequest object and populate it  
		Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
		req2.setComments('Approving request.');
		req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});
		// 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);
		// Verify the results  
		System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());




However, I get a strange error at this line:

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


11:58:17.881|EXCEPTION_THROWN|[90,36]|System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: []
11:58:17.882|FATAL_ERROR|System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: []

Class.TestJISApprovalTrigger.JISValuesonAccountTest: line 90, column 36
External entry point


I have no idea what required field it is referring to, there is no required field missing in the custom object record I've created.


I'd really appreciate some guidance here.


Best Answer chosen by Admin (Salesforce Developers) 

In case anyone else is stuck on this one, I needed to set the approver id for the first request, i.e.


 req1.setNextApproverIds(new Id[] {UserInfo.getUserId()});