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
LudivineLudivine 

Test Approval Request

Hello,

Can you 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.');
        reqc.setAction('Approve');
        // 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             
        reqc.setWorkitemId(newWorkItemIds1.get(0));
        
        // 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.
KevinPKevinP
can you paste your test code? 

When testing approval processes, you'll need to make sure to turn on seeAllData=true.
LudivineLudivine
Hi Kevin,


I have added this on top and it works now!
Thank you very much for your help.