0

I try to approve record automatically after it was send for approval by standard functionality. How can it be done through apex? Here is my code:

trigger ITAssetTrigger on IT_Asset__c (after update) {
    for(IT_Asset__c it: Trigger.new){   
        Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();    
        List<ProcessInstanceWorkitem> pItem = [Select Id, OriginalActorId from ProcessInstanceWorkitem where ProcessInstance.TargetObjectId =: it.id AND ProcessInstance.Status = 'Pending' AND OriginalActorId = '005b0000001SGaeAAG'];
        system.debug('pItem='+pItem);
        if(pItem.size() > 0){                            
            prWkItem.setWorkItemID(pItem[0].id);
            prWkItem.setComments('Request Canceled by User: ' + userInfo.getName());
            prWkItem.setAction('Approve'); 
        }

        if(prWkItem.WorkItemID != null){   
            system.debug('prWkItem='+prWkItem);
            Approval.ProcessResult appResult = Approval.process(prWkItem);
        }
    }
}

This code brings error:

INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []: Trigger.ITAssetTrigger: line 16, column 1". 

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.