Is there any way to re-assign a pending approval request thru apex code (code executing under standard user context i.e. not as sysadmin or without without sharing
)?
I have an Opportunity Approval process that when kicked off will create the approval requests (in Pending
status). The approval process and requests will be stored in ProcessInstance
& ProcessInstance*
sobjects (like ProcessInstanceNode, ProcessInstanceWorkItem etc). Active approval step/ request will have records in ProcessInstanceWorkItem
sobject. I've used the following apex code that will reassign a pending approval request to another user and this code works fine as system admin (or as the current actor of the ProcessInstanceWorkitem
record i.e. current assigned approver), but fails for all other profiles and users.
List<ProcessInstanceWorkitem> piWorkItemsUpdate = new List<ProcessInstanceWorkitem>();
List<ProcessInstanceWorkitem> piWorkItems = [SELECT Id, ActorId, OriginalActorId, ProcessInstance.TargetObjectId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = <Opportunity Record ID>];
for(ProcessInstanceWorkitem piWorkItem: piWorkItems){
piWorkItem.ActorId = <User ID>;
piWorkItem.OriginalActorId = <User ID>;
piWorkItemsUpdate.add(piWorkItem);
}
if(!piWorkItemsUpdate.isEmpty()){
Database.SaveResult[] updateResults = Database.update(piWorkItemsUpdate, false);
}
My requirement is that when a non-admin user changes value in a specific user lookup field in the opportunity record page, the corresponding pending approval request should be assigned to the new user selected in the lookup. So, I had placed the above code in opportunity trigger and this piece of code would run as the user who updates the opportunity record. As given in the following article, the code will work only for system admin or current actor of the ProcessInstanceWorkItem record (current pending approval request): https://help.salesforce.com/articleView?id=000206576&type=1
I've tried the following options, but none worked:
- Using Approval.ProcessSubmitRequest or Approval.ProcessWorkitemRequest doesn't help
- There seems to be no options for enabling/ accessing Sharing settings nor setting OWD for ProcessInstanceWorkitem object or records.
- Tried 'without sharing' keyword on the apex class
- Shared all the records of the current assigned approver to the new approver (from Setup > Users page) with Read/ Write permission.
Any help/ ideas?