2

Following error is popped up when trying to approve the approval step by code.I can sense this error is because of wrong work item id but I am unable to do so.

Approval Process error:

System.DmlException: Process failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []:

Apex Class:

public void approveRecord(SLA_Claim__c opp)
{
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setComments('Approving request using Trigger');
req.setAction('Approve');        
//req.setNextApproverIds(new Id[] {});  I don't need this as requirement is only for last step to approve via code.      
ProcessInstanceWorkitem workItem =[Select Id,ActorId from ProcessInstanceWorkitem  where ProcessInstance.TargetObjectId =: Opp.ID AND ProcessInstance.Status = 'Pending'];

if(workItem.Id == null)
{
opp.addError('Error Occured in Trigger');
}
else
{
req.setWorkitemId(workItem.Id);
// Submit the request for approval
Approval.ProcessResult result =  Approval.process(req);
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.