+ Start a Discussion
vleandrovleandro 

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 this...so 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 coding...so close and yet so far!

 

Thank you!

vleandro

 

@isTest(SeeAllData=true)
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) {
			chreq.ownerid=lstUser[0].Id;
		}
		
		insert chreq;
    	
    	//Create an approval request for the change
    	Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
    	req1.setComments('Submitting request for approval.');
    	req1.setObjectId(chreq.Id);
    	req1.setNextApproverIds(new Id[] {UserInfo.getUserId()});
    	
    	//Submit the approval request for the change
    	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());
    	
    	System.assertEquals(
    		'Approved', result2.getInstanceStatus(), 
    		'Instance Status'+result2.getInstanceStatus());
    }	
}

 

vleandrovleandro

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"?