I try to approve next step of approval process automatically, when submit for approval button is pressed. So, when the button is pressed, it updates two fields on record and then trigger is run. I try in different ways, but my code throws exception: INVALID_CROSS_REFERENCE_KEY in line Approval.ProcessResult[] appResult = Approval.process(prWkItems);

What I am doing wrong?

My code is below (commented are not working):

trigger ITAssetTrigger on IT_Asset__c (after update) {

    for(IT_Asset__c it: Trigger.new){   

        Approval.ProcessWorkitemRequest[] prWkItems = New Approval.ProcessWorkItemRequest[]{};
        ProcessInstance[] pi = [Select ID, Status, TargetObject.Name, (SELECT Id, ActorId, ProcessInstanceId FROM Workitems), (SELECT Id, StepStatus, Comments FROM Steps) From ProcessInstance Where TargetObjectID = :it.Id AND Status = 'Pending'];    

        for(ProcessInstance instance : pi){      
            for(ProcessInstanceWorkItem workItem : instance.WorkItems){                
                Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();

                prWkItem.setComments('Request Canceled by User: ' + userInfo.getName());

            Approval.ProcessResult[] appResult = Approval.process(prWkItems);
  • Clean up the code and post relevant code which is related to the issues – Santanu Boral Mar 22 '19 at 11:36
  • @SantanuBoral, cleared – Viktor Mar 22 '19 at 13:07

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.