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
KMK91KMK91 

Errors with Unit Test for Approval Process Batch class

Hi Team,
I'm facing below error.
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProcessDefinitionId, CurrentNodeId]: [ProcessDefinitionId, CurrentNodeId]

Thanks
KMK
Best Answer chosen by KMK91
Shubham_KumarShubham_Kumar
Hi KMK
Seems like you are trying to process new Approval.ProcessSubmitRequest and didn`t provide the processDefinitionId. As i can`t see your code here`s a sample aprroval process that i have used in my test class.
 
Approval.ProcessSubmitRequest req1 =
new Approval.ProcessSubmitRequest();
        req1.setComments('Approved.');
        req1.setNextApproverIds(new Id[]{UserInfo.getUserId()});
       
        req1.setObjectId(l.id);//Set the record Id
       
        // Submit on behalf of a specific submitter
        req1.setSubmitterId(UserInfo.getUserId());
       
        // Submit the record to specific process and skip the criteria evaluation
        req1.setProcessDefinitionNameOrId('');//set the name of your approval process
        req1.setSkipEntryCriteria(true);
        // Submit the approval request for the account
        Approval.ProcessResult result = Approval.process(req1);
       
       
        //Step for approving by user 1st
        Approval.ProcessWorkitemRequest req2 =  new Approval.ProcessWorkitemRequest();
        req2.setComments('Approving request.');
        req2.setAction('Approve');
        req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
        system.debug('req2::'+req2);
        // Use the ID from the newly created item to specify the item to be worked
        req2.setWorkitemId(result.getNewWorkitemIds().get(0));
        system.debug('req3::'+req2);
        // Submit the request for approval
        Approval.ProcessResult result2 =  Approval.process(req2);
       
          //Step for approving by user 2nd
        Approval.ProcessWorkitemRequest req3 =  new Approval.ProcessWorkitemRequest();
        req3.setComments('Approving request.');
        req3.setAction('Approve');
        //req3.setNextApproverIds(new Id[] {UserInfo.getUserId()});//UserInfo.getUserId()
        system.debug('req2::'+req3);
        // Use the ID from the newly created item to specify the item to be worked
        req3.setWorkitemId(result2.getNewWorkitemIds().get(0));
        system.debug('req3::'+req3);
        // Submit the request for approval
        Approval.ProcessResult result3 =  Approval.process(req3);
P.S : Please mark this as the best answer if it helped.

Thanks 
Shubham Kumar