+ Start a Discussion

Test Approval Process : Invalid CrossReference Key


Can someone give me the correct statement to get the Id of the Approver in a pending Approval process in my test class?

In my org, when we create an Opportunity, the record is automatically submitted for Approval thanks to the Approval Process found.
I have created a similar action in my test class and I need to add the statement to retrieve the id of the Approver to Approve the record for him.
And there I am struggling...

The error I get in the Apex Test Result :
System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

The error is in there : 
reqc.setNextApproverIds(new Id[] {UserInfo.getUserId()});
but I have tried a lot of things without success...

I use that method :

        // Instantiate the new ProcessWorkitemRequest object and populate it         
        Approval.ProcessWorkitemRequest reqc = new Approval.ProcessWorkitemRequest();
        reqc.setComments('Approving request.');
        // Approver is Marco Isenmann '00520000002DPkK&tab'
        reqc.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 resultc =  Approval.process(reqc);
        // Verify the results             
        System.assert(resultc.isSuccess(), 'Result Status:'+resultc.isSuccess());

Thanks a lot for your help.