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

Submit Approval Test Class

I have a trigger that essentially makes the Comment for an approval required.  That works fine.  I'm now trying to write my corresponding test class and running into a problem.


The error I get when I run the test, is:


Error Message: System.DmlException: Process failed. First exception on row 0; first error: INVALID_OPERATION, Illegal transition type: []

Stack Trace: Class.TestRequireApprovalComment.myUnitTest: line 70, column 1


I ran the test through Eclipse, and of course I come up with it's definately failing by not submitting the approval and processing it correctly; I think.


13:13:22.932 (9932689000)|EXCEPTION_THROWN|[75]|System.AssertException: Assertion Failed: Instance StatusPending: Expected: Approved, Actual: Pending
13:13:22.932 (9932955000)|FATAL_ERROR|System.AssertException: Assertion Failed: Instance StatusPending: Expected: Approved, Actual: Pending


Any ideas, thoughts or suggestions?  I'm pretty new at Apex and close and yet so far!


Thank you!



private class TestRequireApprovalComment {

   static testMethod void myUnitTest() {
	    // Insert a change request
		List<User> lstUser = [select Id from User where IsActive = True and IsStaffUser__c = True limit 1];
		Change_Request__c chreq = new Change_Request__c();
		chreq.FKStatus__c = [select id from Status__c where name like 'OPENED' limit 1].id;
		chreq.Change_Type__c = 'Standard';
		chreq.FKCategory__c = [select id from Category__c limit 1].id;
		chreq.Change_Description__c = 'test';
		if(lstUser.size()>1) {
		insert chreq;
    	//Create an approval request for the change
    	Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    	req1.setComments('Submitting request for approval.');
    	req1.setNextApproverIds(new Id[] {UserInfo.getUserId()});
    	//Submit the approval request for the change
    	Approval.ProcessResult result = Approval.process(req1);
    	//Verify the 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();
    	//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());
    		'Approved', result2.getInstanceStatus(), 
    		'Instance Status'+result2.getInstanceStatus());



For anyone who might be curious....


The "challenge" lie with my Approval Process.  I had two approvers defined and it required "unanimous" agreement before the "Change" could be "approved".


I simplified the approval process to just one approver and now my test class runs.  Alternatively, I guess I could have changed the approval process to "first to approver". 


I'd still be interested in how we get around this in general since any number of approval processes could have any under of approvers or is this "good enough"?